How to fix a 404 with routes in ASP.NET MVC? - c#

I'm having a problem trying to get routing to work with ASP.NET MVC 3.0. I have the following routes declared:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional }
);
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
);
routes.MapRoute(
"TestRoute2",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
When I visit:
http://localhost
The site works correctly, and it appears to hit Default route.
When I visit:
http://localhost/1
I get a 404:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make
sure that it is spelled correctly.
Requested URL: /1
Here are the actions those routes correspond to:
public ActionResult Index3(int? id)
{
Product myProduct = new Product
{
ProductID = 1,
Name = "Product 1 - Index 3",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
};
Product myProduct2 = new Product
{
ProductID = 2,
Name = "Product 2 - Index 3",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
};
ViewBag.ProcessingTime = DateTime.Now.ToShortTimeString();
if (id == 1)
return View("index", myProduct);
else
return View("index", myProduct2);
}
How do I structure my routes so that all three action methods are hit correctly?

ASP.NET MVC Routing evaluates routes from top to bottom. So if two routes match, the first one it hits (the one closer to the 'top' of the RegisterRoutes method) will take precedence over the subsequent one.
With that in mind, you need to do two things to fix your problem:
Your default route should be at the bottom.
Your routes need to have constraints on them if they contain the same number of segments:
What's the difference between:
example.com/1
and
example.com/index
To the parser, they contain the same number of segments, and there's no differentiator, so it's going to hit the first route in the list that matches.
To fix that, you should make sure the routes that use ProductIds take constraints:
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
new { id = #"\d+" } //one or more digits only, no alphabetical characters
);
There are other issues with your set up, but those are two things that come to mind right off the bat.

Your routes MapRoute Default should be the last.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional }
);

Push the most generic route to the last of the MapRoute call chain.
Try this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } // Parameter defaults
//new { controller = "Product", action = "Index2", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"TestRoute2",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Move your Default route to the end, Default route should be the last route to define because it acts as a catch all route

In my case, the answer for the same problem was a matter of needing to "include in project" the relevant controllers and views instead of incorrect routing rules.
When mine were created, they weren't automatically included for some reason. This problem was revealed after I closed and re-opened the solution.
{+1 hate} awarded to Visual Studio for its faulty hyper-automation sending me digging through Web.Config files, trying to tack on extensions, and even trying (and failing) to whip up a decent ErrorController.

Related

How to map route with two parameters value where the second parameter is optional?

I am having trouble in route mapping. I have a ActionResult with two parameter where first parameter(category) is compulsory and second parameter(page no) is optional. Now I want to map a route that can work for url.
ie.
1). http://example.com/Blog/Category/programming
2). http://example.com/Blog/Category/programming/1
where the programming is category and /1 is page no.
Here is my ActionResult:
public ViewResult Category(string category, int? p = 1)
{
int pageNo = 1;
if (p != null)
pageNo = Convert.ToInt32(p);
//other code
return View("Posts", myViewModel);
}
This is my mapped route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Blog", action = "Posts", id = UrlParameter.Optional }
);
//This route does not work
routes.MapRoute(
"MyCategory",
"Blog/Category/{category}/{p}",
new { controller = "Blog", action = "Category", category = "", p = UrlParameter.Optional }
);
}
Now, problem is that when I try to navigate page with both parameter its working fine. but, when i try without page no it gives me error "Server not found."
I have also read some post where they have suggested some solution like this. But, still id doesn't work.
Multiple optional parameters in MVC is not working
/Blog/Category/programming matches the first route (default), which should have no problem invoking the Category action on the Blog controller, except the category parameter would be null, because the third segment of the default route is the id parameter, not category. Do one of the following:
Put the default route last
Rename the category parameter to id

MVC 4 custom routing str and int

I need to have a custom route, like the default one, but this one should accept numeric values as strings. Like 0015. If I leave the parameter type as int, the value passed to the controller method get truncated to 15. And I need 0015.
So what I did, I created the following:
routes.MapRoute(
name: "AccRef",
url: "{controller}/{action}/{acc_ref}",
defaults: new { controller = "Company", action = "Index", acc_ref = "" },
constraints: new { acc_ref = #"^\d{1,4}$" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And the problem is, as I understand, that when I now pass in an integer as "id"
#Url.Action("Method", "Controller", new { id = item.ref})
from the view, the routing still applies the first route to it and the call fails.
How would you go about solving this problem with routing?
Is it possible to have two same routing configurations where one accepts int and another string?
Your AccRef is too greedy.
If you look at the url generated from the Url helper it is:
Controller/Method/id
This matches your first AccRef route as well as the default route.
You have to be more specific with your routes. Also the order you define your routes are important. So you normally want to define greedier routes last.
Phil Haack has a route debugger on nuget (blog post here) which can help you identify route issues.
If you reverse the order like so:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AccRef",
url: "{controller}/{action}/{acc_ref}",
defaults: new { controller = "Company", action = "Index", acc_ref = "" },
constraints: new { acc_ref = #"^\d{1,4}$" }
);
Your current scenario will work with above change but the url "/Home/Index/5" or "/Company/Index/0015" still matches both the routes. This is because the routes are generic (as correctly pointed out by Bigfellahull).
In your case since both the parameter is of type int, both the routes are matched.
Option 1:
You can add a extra string say "Acc" in the route url to make it more specific.
routes.MapRoute(
name: "AccRef",
url: "{controller}/{action}/{acc}/{acc_ref}",
defaults: new { controller = "Company", action = "Index", acc_ref = "" },
constraints: new { acc_ref = #"^\d{1,4}$" }
);
In this case the url will change to ".Company/Index/acc/0015".
Option 2:
If you can change the parameter type in action method like so:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
}
}
The url will match only one route.
Option 1 and 2 are for example only to explain how you can make routes more specific.

ASP.NET MVC 3: Solving a route conflict

I've been writing a blog as a learning project for a while now and I've just rewritten my URL structure in order to improve the organisation of my controllers. This has gone fairly smoothly, but I have a little problem with a conflicting route.
I'm trying to setup my URL structure as follows:
/
/page/2
/category
/category/page/2
The categories are stored within the database. This works fine at the moment, but I just noticed that when I try to link back to the home page that it's hitting /page instead.
Here's my current route table:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Admin",
"admin",
new { controller = "Admin", action = "Index" }
);
routes.MapRoute(
"ShowPagedPostsByCategory",
"{category}/page/{page}",
new { controller = "Posts", action = "Index", page = UrlParameter.Optional },
new { page = #"(\d+)?" }
);
routes.MapRoute(
"ShowPagedPosts",
"page/{page}",
new { controller = "Posts", action = "Index", page = UrlParameter.Optional },
new { page = #"(\d+)?" }
);
routes.MapRoute(
"ShowPostsByCategory",
"{category}",
new { controller = "Posts", action = "Index" }
);
routes.MapRoute(
"ShowTaggedPosts",
"posts/tagged/{tag}",
new { controller = "Posts", action = "ShowTaggedPosts", tag = UrlParameter.Optional }
);
routes.MapRoute(
"EditDeleteComment",
"posts/{action}/{id}",
new { controller = "Posts" },
new { action = #"EditComment|DeleteComment", id = #"\d+" }
);
routes.MapRoute(
"AddComment",
"{controller}/comment",
new { controller = "Posts", action = "Comment" }
);
routes.MapRoute(
"ShowPost",
"{controller}/{PostID}/{*slug}",
new { controller = "Posts", action = "ShowPost", slug = UrlParameter.Optional },
new { PostID = #"\d+" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Posts", action = "Index", id = UrlParameter.Optional }
);
}
I can see what the problem is: the home URL of '/' is matching with the ShowPagedPosts route, but moving that below the default route seems to be the wrong thing to do. That makes me think my approach to this is a bit off. Can anyone point me in the right direction please?
Edit: Actually, with RouteDebugger I can see that it's actually matching the ShowPagedPosts and ShowPostsByCategory routes.
When using Html.ActionLink, the first matching route will be used.
If you want to use another specific route, use Html.RouteLink which takes the route's name as a parameter.

MVC3 not finding resources

I'm trying to start a mvc3 website. But when I load the server I get this error.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make
sure that it is spelled correctly.
My routes file looks like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"States", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "States", action = "Index", id = "" } // Parameter defaults
);
}
I noted that you have two possibly conflicting route registrations. Try removing the first registration and leaving only this one:
routes.MapRoute(
"States",
"{controller}/{action}/{id}",
new { controller = "States", action = "Index", id = "" }
);

MVC 3 How to use MapRoute

Could someone show me how to use the MapRoute method? I have tried creating my own routes, but it's not working. What i want to accomplish is a route that routes "http://servername/home/default.aspx" into controller "Home" and action "Default". Also, would it be possible to say that if the user is browsing the default.aspx "file", it would actually point to the "Index" action?
I have tried reading the MSDN references and googling, but it didn't make me any wiser.
Probably too late to help the developer who raised the question but may help someone else. New to MVC but what I found is the map routes seem to be processed in the order they are added. I had a similar problem, my specific route was not working until I started adding the default route as the last route.
If the default map route is added before your custom one and your custom URL matches the structure defined by the default map route you will never reach your custom route.
The route you want to configure the first part of your question is:
routes.MapRoute(
"",
"home/default.aspx",
new { controller = "Home", action = "Default" }
);
Assuming you wish to 'browse' default.aspx with some sort of parameter you can do something like:
routes.MapRoute(
"",
"home/default.aspx/{param}",
new { controller = "Home", action = "Default", param = UrlParameter.Optional }
);
And you would then need to create your Default action to accept string param.
You also have to make sure the parameter name is the same as the action's parameter name.
Example:
routes.MapRoute(
name: "MyName",
url: "{controller}/{action}/{myParam}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MyController:
public ActionResult MyAction(string myParam = "")
{
}

Categories