MVC routing issue, not falling through as the patterns match [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ASP.NET MVC - Catch All Route And Default Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "CatchAll",
url: "{id}",
defaults: new { controller = "Home", action = "CatchAll", id = UrlParameter.Optional }
);
Is there an easy way of implemeting the following routing pattern. Basically i would like the catch all to kick in if the controller does not exist. The pages are dynamically populated for these pages if they exist in the database. Other wise i'll throw an error. I do NOT want a route that starts with any thing else. for example
routes.MapRoute(
name: "CatchAll",
url: "Caught/{id}",
defaults: new { controller = "Home", action = "CatchAll", id = UrlParameter.Optional }
);
I can get it so that each work individually but to get them to work at the same time is proving a little difficult. I'm guessing i will need to overload something somewhere. There must be a guru out there who knows the answer! :D

These two routes are effectively the same thing as far as matching is concerned. The Default route is really a catch all that directs the user to Home.Index. If the controller or action does not exist a 404 error will be raised. You could redirect 404s to a specific controller in the web config.
This might be helpful:
Returning 404 Error ASP.NET MVC 3

Related

Issue with MVC Routing?

I have an issue in Routing in MFA where link like
Project_Name\Controller\Index is working, whereas
Project_Name\Controller\ is not working
This is happening for only some controllers after being deployed in a server.
I am getting the following error:
403 - Forbidden: Access is denied.You do not have permission to view this directory or page using the credentials that you supplied.
Is there any further configuration values that needs to be considered?
In RouteConfig we define the route path like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So the controller you are using may have Index action method and the URL works.
But if your controller does not contain Index method or it is having parameters then it will not work.
This is a possible solution that you might create an action method named Index in all those controllers.
for more, you need to share some more details.

Narrowing scope of C# MVC Routing to a namespace or partial group of controllers

I have two different categories of endpoints on a single C# site:
1) A normal MVC site serving up html
2) A JSON API
I'm looking for a way to route requests starting with api only to API controllers, and everything else through the normal MVC controllers.
The two groups of controllers are in different namespaces, but apparently when it's building these routes, it looks at all namespaces?
What I have below won't work because, essentially, the second group is too permissive and will route to API controllers, but I'm not sure what to do instead to scope or namespace these requests.
routes.MapRoute(
name: "Api",
url: "api/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);

MVC Controller routing, change the name of controller in url

I've got controller named IHateYou, and got loads of views inside that I can access through typing ...\IHateYou\User1. I need to change the name of controller to Users in url, but the rest stays the same, so the users can still enter it through ...\Users\User1. I've tried adding route, but I could still access it by the prior way.
i think you should add a constraints to your default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "IHateYou", action = "Index", id =UrlParameter.Optional },
constraints: new{ controller= "[^IHateYou]" });
default route alway match controller/action/id

Adding company name in Routes mvc 4

I have been trying to give options to users like Facebook to add their company name in the URL:
http://localhost:50753/MyCompany/Login
I have tried different URLs, but it didn't work.
routes.MapRoute(
name: "Default",
url: "{companyName}/{controller}/{action}",
defaults: new { controller = "Login", action = "Index"}
);
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
Now when I add this route to get it work, all of my AJAX requests start fails and those that succeed represent HTML rather than JSON. What I have noticed is that because of this route, my page gets reload again.
Can someone help me figure out how can it be done using MVC routing (if it's possible, or if I'm thinking in the wrong way)?
The problem you are having is due to the fact that both of these routes will match all URLs that have 1, 2, or 3 segments defined (because the controller and action have default values). Since routes are executed in order from the top route to the bottom route, your top route will always match and your bottom route never will match (except for the home page).
Since the top route always matches, URLs that assume that the first segment is the controller and the second segment is the action will fail because you are putting these values into the companyName and controller route keys, respectively.
For this to work as you expect, you need to make a route constraint that is aware of all of the company names.
routes.MapRoute(
name: "Default",
url: "{companyName}/{controller}/{action}",
defaults: new { controller = "Login", action = "Index"},
constraints: new { companyName = "Company1|Comany2|Company3" }
);
Note that you could implement IRouteConstraint so you could pull the values to match from a cached database model instead of hard-coding them into the configuration. See this post to learn how to create a custom route constraint.
Or, as Andy mentioned, you can make the match unique by specifying 1 or more segments of the URL explicitly.
url: "{companyName}/Login"
The idea is there must be some way to make the first route you defined not match in certain cases.
Alternatively, you could implement RouteBase, but you would only need to if you require much more control over the matching process than this simple scenario.
The problem is there is no way to distinguish between your two routes. For example /a/b/c could be the Default route with company = a, controller = b, action = c or it could be the Login route with controller = a, action = b, id = c.
To solve this you'll need to design your routes, including the ones for AJAX, so that there is no way two routes could have the same URL. In your example you could just drop the /{id} from the login route as it isn't needed. Also specify the URL more specifically and put it before the default. This would give you something like
routes.MapRoute(
name: "Login",
url: "{companyName}/Login",
defaults: new { controller = "Login", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{companyName}/{controller}/{action}",
defaults: new { controller = "Login", action = "Index"}
);
In this case both /MyCompany/Login and /MyCompany/Login/Index would go to the login page. However MyCompany/Home/Index would go to controller = Home, action = Index.
Personally, I tend to remove the default route altogether so I can specify the URLs I want rather than have them all as /controller/action. That gives you more control but does mean specifying each route individually.

MVC4 Index Action Not Working Correctly

I have created a controller with an Index action. All of my other Actions return the views just fine...but for some reason I have to specify the full url to have the Index view return. It's almost like my Routes aren't working correctly.
For instance, to go to the properties page, you have to go to /Properties/Index instead of just /Properties/. My routes are as follows. Any help would be greatly appreciated!
routes.MapRoute(
name: "Index",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
For security reasons in ASP.NET in general, you can't have a "Properties" path. C# projects all come with a Properties folder by default and ASP will ignore it when accessed directly to prevent file access to it.

Categories

Resources