Why does this route not resolve? - asp.net-mvc-3

routes.MapRoute(
"top", // Route name
"{controller}/Casestudy/{action}"
);
Anyone know why this routing doesn’t resolve to;
/Auction/Casestudy/ABC/
The controller Auction is there, the Action ABC is there and the view is there?

My guess is that you put your "top" route after your "Default" route (provided you didn't change that call to MapRoute()).
You need to ensure that your "top" route appears before you Default route, otherwise your "Default" route will surely match your tested URL and deliver a 404.
routes.MapRoute(
"top", // Route name
"{controller}/Casestudy/{action}"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
If those are switched up and your "Default" route comes first, then you'll have your route matching the "Default" route with controller = "Auction", action = "Casestudy", id = "ABC". Which is obviously not the intention.

Related

MVC3 :: Using global.asax to direct a specific controller/action to a different controller/action

In an MVC3 app I have a view in my home controller that is accessed via the following URL:
http://mysite.com/home/mycontent
However, my user wants to accees it via:
http://mysite.com/mycontent (without a specific reference to the home controller)
I've tried modifying my global.asax with the following:
routes.MapRoute(
"MyContent", // Route name
"MyContent", // URL with parameters
new { controller = "Home", action = "MyContent"} // Parameter defaults
);
But I get 404 errors when I try to use it. Is there a way I can accomplish what my user is wanting to do - redirecting a URL to a specific controller/view pair?
I think you should keep some "parameters":
routes.MapRoute(
"MyContent", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "MyContent", id = ""} // Parameter defaults
);
In what order are your routes registered? That route should come before your default route.
E.g.
routes.MapRoute(
"MyContent", // Route name
"MyContent", // URL with parameters
new { controller = "Home", action = "MyContent"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
Otherwise, it should work... I have routes defined exactly the same as that.

MVC 3 Changing Home route

It's my first MVC project and my customer doesn't want any links like xxx.com/Home/Index or something like that. When i change my controller name the browser looking for /Home and it gives me an ex. Bec there's no HomeController.
How can i change it the default controller "Home" with another one.
Just change your Default route:
Default route created in Global.asax (Unchanged)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Modify the defaults for the parameters (controller, action, etc.). Something like this:
Modified Default route (Changed)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "YourNewController", action = "SomeOtherAction", id = UrlParameter.Optional } // Parameter defaults
);
The routes are set in the Global.asax file in the RegisterRoutes(RoteCollection routes) routine. The default route is specified as:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You would need to change the "Home" part to your new controller name.
You can also set up additional routes as required (see here).

Controller not being properly activated

I have added a controller to my project named UserManager (automatically generated from the ado.net framework)
When I start the application, attempts to navigate to http://server/UserManager/ are met with a 404 error, but if I go to http://server/UserManager/Index the action is found and executes properly.
Is this a case of the controller not being called or is it just not treating index as the default action. Where are these properties set?
UPDATE
It seems that the problem derived from the fact that the default route is set to
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Permits", action = "ListApplications", id = UrlParameter.Optional }
This conflicts with the naming scheme for Usermanager (where the default is Index)
I struggled with ohow to add alternate routes that provided for default actions, but eventually figured out that the order of route addition determines which route takes the request (the earlier the route is added, the more chances it has to meet the criteria.)
Thanks
You need to ensure that the default route mapping specifies "Index" as the default action in your global.asax file.
Check that you have the following setting in your global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
IN REPLY TO YOUR COMMENT:
Only by way of adding new route mappings. You also need to be aware that the first matching route mapping will be applied, so the order you specify the mappings in Global.asax is crucial.
For example, we wanted our FAQ controller to work with a URL http://domain/faq/{id} without the action specified in the URL, so we declared the following mapping before the default:
routes.MapRoute("Faq", "Faq/{id}", new { controller = "Faq", action = "Answer" });
In Global.asax.cs, check the default route is set up:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional}
);
Also, check that the controller is called UserManagerController, and derives from Controller

ASP.Net MVC Routing: How to catch the root of a website?

In my global.asax I have three routes:
//MemberHome is supposed to handle urls like http://localhost/johndoe
routes.MapRoute(
"MemberHome", // Route name
"{username}",
new { controller = "PublicMember", action = "Index", username = "username" }
);
//Home is supposed to catch http://localhost/
routes.MapRoute(
"Home",
""
);
// the default way of doing things..
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem is, I can get them to work separtely (just home or just MemberHome)
But when they are both activated its either Home or MemberHome which gives me a 404 resource not found..
Any idea how I can get this work?
You need to set the first part of the route to something distinct. Eg:
routes.MapRoute(
"MemberHome", // Route name
"MemberHome/{username}",
new { controller = "PublicMember", action = "Index", username = "username" }
);
or:
routes.MapRoute(
"Default",
"Home/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
That way they will be distinct. Whichever one is given the fixed part of the route should come first, as otherwise the route handler will match the wildcard one first...

ASP.NET MVC 3.0 Url Rewritng

I have Action called Contact in home controller
<mysite>/Home/Contact
I want to be able by typing <mysite>/Contact to get the same result as <mysite>/Home/Contact
Is it possible to do with mvc 3.0 routes or RouteMagic?
Currently i am trying to achieve this like that, but no luck:
Custom Routes:
routes.MapRoute(
"Contact", // Route name
"Contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
RouteMagic:
var route = routes.MapRoute("new", "Contact");
routes.Redirect(r => r.MapRoute("old", "Home/Contact"))
.To(route);
Update
Ok the custom routes should be defined first, now it is working(in case of custom routes), but there is appeared a new question why route magic returning error:
Server Error in '/' Application.
Value cannot be null or empty.
Parameter name: controllerName
Make sure your new route occurs before the default route (since it will match as well) when defining the route.
routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
have you tried the rewrite module in iis7 ?
its easy to use , donwlod it from here:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Resources