Asp.net mvc routing without a controller or action name - asp.net-mvc

I am trying to redirect all urls that don't match an existing controller to a certain controller.
For example, the url mywebsite.com/newyork should be processed as mywebsite.com/Cities/Info/newyork
I am using the following code in my RegisterRoutes but it doesn't seem to work as I get a 404 reponse:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info", cityname= "" }
);

You should put your cities route first and drop the empty default parameter:
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The routes are processed in order so you should have most specific first to least specific ( your default route).
As your website.com/newyork matched the default route, it wasn't continuing to your city route.

Related

How to create an alternative route for default in .NET framework

I have a default route value that is getting overridden by a custom one.
routes.MapRoute(name: "company-portal-program", url: "{companyName}/Programs/{programName}", defaults: new { controller = "Portal", action = "Program" });
routes.MapRoute(name: "company-portal", url: "{companyName}", defaults: new { controller = "Portal", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This is obviously because they overlap.
I would like the default route to take precedence and if the controller is not found the company-portal route should be used, is this possible?

Routes in ASP.NET MVC 5

My requirement is I need to set multiple routes to same controller/action method.
If user enters url http://localhost:xxxx/home/index , it will target "index" action method of "home" controller.
I also want "http://localhost:xxxx/products" and "http://localhost:xxxx/categories" to point to "index" action method of "home" controller.
I was able to achive this by adding two routes "categories" and "products" as mentioned below , and it is working fine.
routes.MapRoute(
name: "categories",
url: "categories",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "products",
url: "products",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
My question is , is there any way I combine those two routes "categories" and "products" in to one ?
You can achieve this by adding a constraint to your route.
Make the entire path a parameter, and then assign a regular expression rule to match this parameter.
routes.MapRoute(
name: "IndexMapper",
url: "{alternateIndexName}",
defaults: new { controller="Home", action="Index" },
constraints: new { alternateIndexName="(categories)|(products)"}
);
https://msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_6

MVC Routing not working for particular value

I have this route configuration
routes.MapRoute(
name: "catProducts",
url: "Description/{action}/{id}",
defaults: new { controller = "Home", action = "ProductDetail" }
);
routes.MapRoute(
name: "Products2",
url: "Category/{action}/{cat}/{subcat}",
defaults: new { controller = "Home", action = "Products", subcat = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Account", action = "Login"}
);
routes.MapRoute(
name: "Default1",
url: "{controller}/{action}"
);
When I am trying
http://localhost/B2BWebSite/Description/ProductDetail/3
it's somtime redirect right but sometime goes to login page
There is no specific route defined for B2BWebSite. Hence when you go to http://localhost/B2BWebSite/Description/ProductDetail/3 it checks for default route and your Default route is Account/Login as below. Which is why it goes to login page.
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Account", action = "Login"}
);
Regarding, you question about sometimes it works and sometimes doesn't; probably has something to do with login information in the session. If user is logged in, it might not go to login page.
You may need to specify id in the first route.
routes.MapRoute(
name: "catProducts",
url: "Description/{action}/{id}",
defaults: new { controller = "Home", action = "ProductDetail", id= UrlParameter.Optional }
);

ReturnUrl is not formed properly while accessing one of my controller?

I have one controller Jobs when I'm trying to access this controller without logging to my portal, return url is not formed properly and it looks like
http://test-employer.cloudapp.net/accounts?ReturnUrl=%2F
and for accessing any other controller its formed properly
http://test-employer.cloudapp.net/accounts?ReturnUrl=%2Fusers
Below is my routing config
routes.MapRoute(
name: "JobFeed",
url: "jobs/{q}/jobfeed.xml",
defaults: new { controller = "Jobs", action = "Feed", q = UrlParameter.Optional }
);
routes.MapRoute(
name: "JobPostingFeed",
url: "postings/{q}/jobfeed.xml",
defaults: new { controller = "Postings", action = "Feed", q = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

ASP.NET MVC route

I have only default route enabled:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Which will resove paths like: hostname/Home/Index, hostname/Home/Foo, hostname/Home/Bar/23 just fine.
But I must also enable route like this: hostname/{id} which should point
to:
Controller: Home
Action: Index
{id}: Index action parameter "id"
Is such a route even possible?
If id is a number you could add a route above the other one and define a regex constraint:
routes.MapRoute(
name: "IdOnlyRoute",
url: "{id}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { id = "^[0-9]+$" }
);
Not Tested :
Create your route like this
routes.MapRoute(
name: "custom",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
under your default route
Since I'm short on time, I have configured route for every action. Luckly there are not many of them. :)
routes.MapRoute(
name: "IndexWithParam",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "IndexWithOutParam",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Login",
url: "Home/Login",
defaults: new { controller = "Home", action = "Login" }
);
So last route was repeated for ever other action. Not sure if it can be done better, but it works.
Have you tried to play around with AttributeRouting package?
Then your code will look like this
[GET("/{id}", IsAbsoluteUrl = true)]
public ActionResult Index(string id) { /* ... */ }

Resources