MVC routing is not working sometimes - asp.net-mvc-routing

I have routing in MVC 5 as:
context.MapRoute(
"cart_contact",
"Cart/MyContact",
new { controller = "Shopping", action = "MyContact" }
);
context.MapRoute(
"cart_shipping",
"Cart/MyShipping",
new { controller = "Shopping", action = "MyShipping" }
);
context.MapRoute(
"cart_payment",
"Cart/MyPayment",
new { controller = "Shopping", action = "MyPayment" }
);
context.MapRoute(
"cart_default",
"Cart",
new { controller = "Shopping", action = "Index" }
);
when I have Url as: /Cart/MyContact, sometimes I'm hitting "MyContact" action, sometimes I'm hitting "Index" action, "MyShipping" and "MyPayment" has same problem too, sometimes hitting the "MyShipping" or"MyPayment" action, but sometimes hitting "Index" action, could anybody help me check what's wrong with my routing? what I want is those different endpoint should always hit different action.
Your help is great appreciated.
Thanks
Oreo

Related

Understanding Default URL in MVC Routing

suppose if I have a route in my RouteConfig.cs file like this
routes.MapRoute(
"pattern1",
"{controller}/App{action}",
new { controller = "home", action = "index" }
);
and when I run my application I get an error HTTP Error 403.14 - Forbidden
but when I add this routes below like this
routes.MapRoute(
"pattern1",
"{controller}/App{action}",
new { controller = "home", action = "index" }
);
routes.MapRoute(
"pattern2",
"{controller}/{action}",
new { controller = "home", action = "index" }
);
then i am able to land to the Default page Home/Index.
My question is why am I not able to land to the default page when I have only one routing configured as shown in the first example.
I have configured the default controller to be Home and action to be Index, then to why am I getting error. Am I missing some concept of MVC routing technique.

Area in MVC3- RedirectToAction is not working as expected

I have an area called coverage.The routing is like
context.MapRoute(
"CoverageSummary", // Route name
"Coverage/Summary/{policyId}", // URL with parameters
new { controller = "Coverage", action = "Summary", policyId = UrlParameter.Optional }, // Parameter defaults
new string[] { "Web.Mvc.Claims.Areas.Coverage.Controllers" }
);
when I acess the page Mysite/Coverage/Summary/10 it shows a page. fine.
But in an Action methode i have code as below
return RedirectToAction("Summary","Coverage", new RouteValueDictionary(new { policyID = 10 }));
but this is not loading the page Mysite/Coverage/Summary/10 .
it is showing 404 error.
if i refresh the page still it give 404.but if i cut and past the same url in address bar and hit enter it works
What can be the reason
Try with area property in your RouteValueDicitionary
return RedirectToAction("Summary","Coverage", new RouteValueDictionary(new { policyID = 10, area = "Your_Area_Name" }));

MVC3 Razor routing from Area to Root

I have searched the site and cant find anything similar to my question. I am new to MVC so I may be asking a dumb question.
When the site is launched there are default Home and About buttons at the top. I added a new button that points to an Area, that works just fine.
When I hover over the Home and About buttons after I go to the View that is in my Area, they links on them show they are now pointing to my Area and not the root of my project.
How do I tell these buttons to still point to the root and not my Area?
Thanks for any help or words of wisdom someone will be providing.
This is supposed to take you to root folder.
#Html.RenderAction("Action", "Controller", new { area = "" });
Okay I forgot to ask you what the routes look like in your global.asax.cs file? I've got a similar setup, and here are my route definitions
routes.MapRoute(
"Home", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "MaintainLists",
id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // defaults
);

Custom IRouteHandler route screws up MVC link building

I have a custom routehandler in ASP.NET MVC2 to catch all url's at a prefixed path like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new Route("#api/{*all}", new ApiHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Routing works fine, but if i use Html.Actionlink or return ReturnToAction() from a controller, the uri built creates a broken uri like this:
/#api?action=Add&controller=Home
instead of
/Home/Add
How can i influence the uri building logic to consider the Default route pattern?
I've come up with a hack to stop the described behavior. While this "solves" the problem, it's really kind of nasty and I'd prefer a cleaner way to exclude a route from the virtual path building logic
routes.Add(
new Route("#api/{*all}",
// A random and unlikely controller name as the default
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
// and a constraint requiring any controller to be that random, default value
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
new ApiHandler()
);
This means that the route would only be chosen for Virtual Path building if the controller in question was "qwewqewqeqweq", which hopefully is unlikely. I said it was nasty.
You could specify the name of the route using the RouteLink helper:
<%: Html.RouteLink("link text", "Default",
new { action = "add", controller = "home" }) %>

Custom route doesn't find controller action

For some reason my application isn't routing to my controller method correctly. I have a routelink like this in my webpage -
<%= Html.RouteLink("View", "Blog", new { id=(item.BlogId), slug=(item.Slug) }) %>
In global.asax.cs I have the following routes -
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MoreBlogs",
"Blog/Page/{page}",
new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
"Blog",
"Blog/View/{id}/{slug}",
new { controller = "Blog", action = "View"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And then I have a class BlogController that has a method -
public ActionResult View(int id, string slug)
{
... etc.
}
I put a breakpoint in the first line of the View method but it's not getting hit at all. I checked with a route debugger for the format localhost/Blog/View/1/test and it matched my custom route. All I'm getting is a 404 while running this, I can't work out why the route won't post to the view method in my controller - any ideas?
(Assuming the route debugger was based on Phil Haack's debugger post)
If you're getting a 404, that makes me think the actual view page itself cannot be found.
Assuming that you are also using areas within the application, and that RouteLink is actually being called from within an Area, I think you may need to specify area="" in your routeValues object ( you also need to specify the controller). In general, I think that you will need to add the area="..." part with all your Routelink calls when using Areas.
This is something that I have picked up about RouteLink and Area, but cant seem to find any reference material detailing the limitations.