MVC 3 Changing Home route - asp.net-mvc-3

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).

Related

Creating a simple site to display my apps, having some routing issues

I want to create a site where I display my apps. I use MVC 3.
My Home controller is a typical overview page where I'll list all my apps. Then there is the App controller, where if you click on a app on a overview could get more info... url scenario would be:
http://myhost.com <-- overview
http://myhost.com/App/app_name <-- render info about that app
But I'm having a small problem understanding routes, thought i did it right:
routes.MapRoute(
"App", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And how do I get a hold of id in the controller?
Would your app rule not be more specific, that is
routes.MapRoute(
"App", // Route name
"App/{id}", // URL with parameters
new { controller = "App", action = "Index",
id = UrlParameter.Optional }
// Parameter defaults
);
Note replaced {controller} with App

mvc3 controller not working - is path mapped wrong

Here is my code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Admin", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
}
for first link it works well if i go to:
localhost/song
localhost/date
etc. it opens all links under home controller.
But for second maproute:
localhost/admin
localhost/admin/index
- these link are not working? Can anyone please tell me what i am doing wrong?
First, your default route must be last in the list, not first.
Second, You have two default routes. There is no way for MVC to know which one to use, so it always chooses the first one that matches. Instead, your Url for the admin one should be "Admin/{action}/{id}"

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.

Why does this route not resolve?

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.

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