how to change controller name in url in asp.net mvc - asp.net-mvc

I have a "HomeController" and my route is like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action ="Index", id = UrlParameter.Optional
}
);
I want url be like "web-design/Index" instead of "Home/Index".
how can i do that ?
thanks

I would do it like this:
// add a new route
routes.MapRoute(
name: "homepage",
url: "web-design/{action}",
defaults: new {
controller = "Home", action ="Index"
}
);
// add your default route but change the default action or controller
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action ="SomeotherAction", id = UrlParameter.Optional
}
);

If you want "web-design/Index" to be the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "web-design", action ="Index", id = UrlParameter.Optional
}
);

Related

RedirectToAction direct to default controller action

I want to change my login redirect from
return RedirectToAction("Index", "Home");
To
return RedirectToAction("Foo", "Profile");
The Foo action return a view.
The problem is that I still get the home/index.
Do I need to change the routing?
The RouteConfig is like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "NotFound",
url: "{*catchall}",
defaults: new { controller = "Home", action = "Index" }
);
}
The problem is that the redirect after login redirect to the root '/' url.
You can reset this via your routes.
Something like this below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Foo", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "NotFound",
url: "{*catchall}",
defaults: new { controller = "Profile", action = "Foo" }
);

Asp.net mvc routing without a controller or action name

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.

how to rewrite mvc routes to aspx

I have home controller and Index action method. The below url works
http://localhost/home/index
Will it be possible to make it work like below
http://localhost/index.aspx
I am trying below code in Global.asax but does not works
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
Route Config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
routes.MapRoute(
name: "Customized",
url: "{action}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Reports",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
}
You should use MapRoute() instead of MapPageRoute(), as you are still referring to an MVC controller/action:
routes.MapRoute(
name: "Default2",
url: "index.aspx",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
PS: Remember to register the new route before other ones which may eventually interfere with it.
You can't MapPageRoute for non static content as you are doing. If you wan't to hide the Controller for your route the Customized already do this. If you are mixing MVC + WebForms you should fallow this guide to se how config your routes.
How to: Define Routes for Web Forms Applications

MVC route mapping: 1 URL pattern works only for exactly 1 controller

routes.MapRoute(
name: "GetAdressen",
url: "{controller}",
defaults: new { controller = "AdressenController", action = "GetAdressen"}
);
routes.MapRoute(
name: "GetEinsaetze",
url: "{controller}",
defaults: new { controller = "EinsaetzeController", action = "GetEinsaetze"}
);
In this case only /Adressen will work, and not also /Einsaetze
routes.MapRoute(
name: "GetEinsaetze",
url: "{controller}",
defaults: new { controller = "EinsaetzeController", action = "GetEinsaetze"}
);
routes.MapRoute(
name: "GetAdressen",
url: "{controller}",
defaults: new { controller = "AdressenController", action = "GetAdressen"}
);
In this case only /Einsaetze will work, and not also /Adressen
Why?
Use Like This
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "projectName.Controllers" });

Routing not working

There are regular pages on the site such as:
foo.com/about
foo.com/contact
I use single controller for these.
There is also pages for admin, i have a seperate controller:
foo.com/admin/createsomething
This is my routing config and it is not picking HomeController Actions.
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
How can i fix it?
You need routes for the urls that break the default convention:
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
then your admin url should resolve with the default route, where you have an AdminController and a CreateSomething action method

Resources