1

Very new to ASP.net MVC and C# in general. Experience with PHP/NodeJS mostly, a little Java.

I have a method in a controller like so:

public ActionResult ImageProcess(string fileName){
  string url = "http://myurl.com/images/" + fileName + ".jpg";
  //Code to stream the file
}

And when I navigate to it as "http://myurl.com/Home/ImageProcess/12345" I get thrown a 404 error by the process as it's trying to fetch the file.

If I hard-code it like so...

public ActionResult ImageProcess(string fileName){
  string url = "http://myurl.com/images/12345.jpg";
  //Code to stream the file
}

...It works just fine, returns my processed image as expected.

Why is this happening?

4
  • Do you have any custom routing (if you are not sure, then probably not)? Commented Feb 11, 2016 at 16:05
  • What is actually throwing the 404 then? The request for the ImageProcess action, or a later request to the url value? The description makes it sound like the latter. If that's the case, then what is the value of url that causes the error? Commented Feb 11, 2016 at 16:06
  • So is fileName being returned as null? Not sure how you're attempting to set the parameter, but if it's in a form, the issue could be a number of things, including not having an input with the name of fileName. Commented Feb 11, 2016 at 16:08
  • There are a lot of unknowns here that make it difficult to help. I think we need to know how you're trying to pass fileName in here. Commented Feb 11, 2016 at 16:10

2 Answers 2

7

If you're using the default routes provided for ASP.NET MVC, the fix is simple: change fileName to id.

Example:

public ActionResult ImageProcess(string id) {
  string url = "http://myurl.com/images/" + id + ".jpg";
}

In the file RouteConfig.cs you should see something like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "YourProject.Controllers" }
);

This is the configuration that tells the framework how to interpret URL strings and map them to method calls. The parameters for these method calls need to be named the same as in the route.

If you want the parameter to be named fileName, just rename {id} to {fileName} in RouteConfig.cs, or create a new route with a new name and defaults above the default route. But, if that's all you're doing, you might as well stick with the default route and name the parameter id in your action.

Your other option would be to use a query parameter, which would not require any route or variable changes:

<a href="http://myurl.com/Home/ImageProcess?fileName=yourFileName">link text</a>

Look here for a nice tutorial on routing.

Sign up to request clarification or add additional context in comments.

8 Comments

But then why would the second version in the question work? The action signature wasn't changed.
@David because string is nullable, so it will have a value of null.
Right, but why would the same route return a 404 in one case but not another? I think the OP needs to specify what exactly is producing the 404 error. The same routing would be taking place in both scenarios (the "working" and "not working" versions). The only difference would be what value is in fileName, but the question doesn't contain any information about what that value is or how it was sent there.
@David I was under the impression that the action was being called, but the file could not be found, because his fileName variable is null.
Took 10 for lunch, got a TON of response. Yes, the resulting URL (the url formed in the url parameter) is the one returning the 404. Major kudos for the explanation, lesson learned on routing
|
0

Either change the routing value as @johnnyRose already suggested or change the url to a get parameter, that will let the model binding find the fileName attribute. Like this:

http://myurl.com/Home/ImageProcess?fileName=12345

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.