Remove the controller name in MVC - asp.net-mvc

I have 2 views named "Index" and "Contact" in "Root" folder.I have done following Route in global.asax.`
Controller:rootController
View:Roor/index.cshtml,Roor/Contact.cshtml
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Contact",
url: "root/Contact",
defaults: new { controller = "Root", action = "Contact", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Root", action = "Index", id = UrlParameter.Optional }
);
}
When I run The URL is http://localhost:8889/ is loading Index page.
but when i run for http://localhost:8889/Contact is getting 404 error. When I run localhost:8889/Root/Contact is successfully loading.
I want to run Contact without root in URL.

Try removing root/ from your Contact route like following.
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Root", action = "Contact", id = UrlParameter.Optional }
);

Related

custom routing not working in MVC

I am creating a friendly URL so i have crete route in routeconfig file as follows
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ShowUser", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
and action link as follows
View
But it is not working giving runtime error "The resource cannot be found."
Default route and you custom route have identical structure, so default one will always be used because it is declared first.
Im guessing you are getting error because in code you are referring to ShowUser, but instead you should use name of the controller: UserGroupEdit.
View
If you want a friendly route you can do something like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ShowUser", // Route name
url: "ShowUser/index/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
}
in your cshtml
#Url.Action( "Index", "UserGroupEdit")
You can use UrlHelper.RouteUrl helper method which allows you to specify the route name:
View
RouteConfig:
routes.MapRoute(
name: "ShowUser",
url: "ShowUser/{action}/{id}",
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional }
);
Reference link

How can I render the Index view of a controller without Index in the URL?

I have a Login controller with an Index view that I want rendered at the url http://sitename/login. It works with I browse to http://sitename/login/index but I want to omit index from the URL. Here's my route config (default):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
}
.. and the controller:
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
}
If you had left the Default route alone, it would do what you ask. You just have to set the default value for the action route value as Index.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Alternatively, you can insert an additional route to cover your Login controller.
routes.MapRoute(
name: "Login",
url: "login/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
A third option is to rename your LoginController.Index method to LoginController.Dashboard_1, since that is your default action name for every controller in the application.

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" }
);

MVC Route Configuration: Multiple route with different route name

I have created multiple routes with different route name in MVC.
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
I am calling route from a javascript function to redirect to this route
var url = '#Url.RouteUrl("PostDetails", new { id = "_id_", item = "_name_" })';
url = url.replace("_id_", id).replace("_name_", name);
window.location.href = url;
This is giving an error with 404.
You must add your route before Default route in the RouteConfig like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

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

Resources