Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:44:14+00:00 2026-06-14T03:44:14+00:00

I am using the FileUpload sample from the asp.net tutorials. When I build it

  • 0

I am using the FileUpload sample from the asp.net tutorials. When I build it as a stand alone, it works fine. However, whenever I try to add that functionality to a new MVC4 website, the routing is wrong. I’m probably not explaining this well, so here is the code:

[HttpPost]
public async Task<HttpResponseMessage> PostFile()
{
    // Check if the request contains multipart/form-data.
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    try
    {
        var sb = new StringBuilder(); // Holds the response body

        // Read the form data and return an async task.
        await Request.Content.ReadAsMultipartAsync(provider);

        // This illustrates how to get the form data.
        foreach(var key in provider.FormData.AllKeys)
        {
            var strings = provider.FormData.GetValues(key);
            if (strings != null) foreach(var val in strings)
            {
                sb.Append(string.Format("{0}: {1}\n", key, val));
            }
        }

        // This illustrates how to get the file names for uploaded files.
        foreach(var file in provider.FileData)
        {
            var fileInfo = new FileInfo(file.LocalFileName);
            sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length));
        }
        return new HttpResponseMessage
        {
            Content = new StringContent(sb.ToString())
        };
    }
    catch (System.Exception e)
    {
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
    }
}

Here is the page I’m using:

<div style="height:400px;">
<h3>File Upload</h3>
<form name="trip_search" method="post" enctype="multipart/form-data" action="api/upload">
    <div>
        <input type="radio" name="trip" value="round-trip"/>
        Round-Trip
    </div>
    <div>
        <input type="radio" name="trip" value="one-way"/>
        One-Way
    </div>

    <div>
        <input type="checkbox" name="options" value="nonstop" />
        Only show non-stop flights
    </div>
    <div>
        <input type="checkbox" name="options" value="airports" />
        Compare nearby airports
    </div>
    <div>
        <input type="checkbox" name="options" value="dates" />
        My travel dates are flexible
    </div>

    <div>
        <label for="seat">Seating Preference</label>
        <select name="seat">
            <option value="aisle">Aisle</option>
            <option value="window">Window</option>
            <option value="center">Center</option>
            <option value="none">No Preference</option>
        </select>
    </div>
    <div>
        <input type="submit" value="Submit" />
    </div>

</form>

When I directly navigate to localhost:13927api/upload I see a response from the web api method. I’ve got the DefaultApi route registered in my WebApiConfig.

But when I’m on the page localhost/Home/About and I click on the submit button, it attempts to go to localhost/Home/api/upload – which does not exist.

What am I missing?

EDIT

The suggestion by Mario fixed my issue. The action method on my form was not relative to the root.

action="api/upload" vs. action="/api/upload"

That fixes my problem.

A bit of elaboration on the issue:

When you are in the default path (say yoursite/Home/Index -> if that is your default), then the action=”api/myaction” will work because the current path is still seen as the root of the website. However, once you actually navigate to a path (say yoursite/Home/About), the current path is now under “Home” and so my missing “/” naturally was relative to my current path rather than the root. This is why the samples work without the leading “/”, because the view in question is the default view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:44:16+00:00Added an answer on June 14, 2026 at 3:44 am

    Added the answer, if that can help others too:

    Your form action="api/upload" is not relative to the root. Try action="/api/upload" or use one of the helpers to specify the route.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings! I am using the ASP.NET FileUpload control to allow users to upload text
I am using ASP.NET to read the data in the excel file. I am
Following on from my post.. Saving an image from HTTP POST (Not using FileUpload
I am using ASP.NET 3.5 and C# for developing a web application on helpdesk.
I am developing an asp.net application using .NET 4.0 I have a total of
I'm using an NSMutableURLRequest to perform a simple file upload from iOS5 to a
I want full path of selected file using GWT by using FileUpload. Can you
I am using rich:fileUpload to upload files. When i click the + button to
I am trying to upload a file using commons-fileupload-1.1.1.jar file (I know it is
I'm using Spring and Hibernate. I'm uploading images using commons-fileupload-1.2.2 as follows. String itemName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.