Asp.net mvc url routing. Change url - asp.net-mvc-4

My controller is "/Home/Index".I want, when i type url to "/Dashboard/Index" or "example.com/Dashboard" my "/Home/Index action method" executed. Can you give me starting point?

Just add this route to your route config. Make sure to add it before the default route:
routes.MapRoute(
name: "HomeRoute",
url: "Dashboard/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Related

C# MVC4 Default Controller Redirect

I have two controllers: Default and Page. When the user hits the root http://localhost:1212 I want to redirect the user to Page Controller.
I used this code but no luck:
routes.MapRoute(
"Default",
"",
new { controller = "Page", action = "Get"}
);
Any ideas...?
Just change the original default in RouteConfig to look like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Page", action = "Index", id = UrlParameter.Optional }
);
Make sure that's the last entry if you're mapping other routes

supporting multi-tenant routes in mvc

I'm experimenting setting up a multi-tenant solution in asp.net mvc 4 wherein you are able to specify tenant specific overrides to certain controllers if they require different functionality.
I'd like to have routes that are like
/{Controller}/{Action}/{Id}/
/{Tenant}/{Controller}/{Action}/{Id}
If the tenant isn't specified it should just match the first route.
I have tried
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Tenant",
url: "{tenant}/{controller}/{action}/{id}",
defaults: new { tenant = "", controller = "Home", action = "Index", id = UrlParameter.Optional });
This works correctly for
/ -> detected as the first route (correct!)
/Home -> detected as
first route (correct!)
/Home/Index -> detected as first route
(correct!)
/Client1/Home/Index - Client1 is detected as controller
name (incorrect)
If I switch the routes around then the tenant route works but the base one does not.
What am I missing here, is it possible to achieve this?
Note, I'd prefer to not have to specify a dummy tenant string in the route as I'll have to translate that back later on in a few places where I need to resolve tenant specific information.
You can use the library I wrote here. This lib allows you to define an alternative route, if the routes conflict. You must define the routes as follows:
var firstRoute = routes.MapReplaceableRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
var secoundRoute = routes.MapRoute(
name: "Tenant",
url: "{tenant}/{controller}/{action}/{id}",
defaults: new { tenant = "", controller = "Home", action = "Index", id =
UrlParameter.Optional }, lookupParameters: new string[] {"tenant"}, lookupService: new LookupService());
firstRoute.AlternativeRoute = secondRoute;
For lookupService, you just need an empty implementation of the IRouteValueLookupService.

// GET: /Article/{text} in MVC?

I have a controller named ArticleController with an Index method that returns the Article view. This works.
However, I'd like to be able to process any text after Article/ in the URL such as Article/someText Article/fileNanme Article/etc
I thought this would be straightforward by implementing the following:
// GET: /Article/{text}
public ActionResult Index(string someText)
{
...
}
This doesn't work. Any ideas?
Update:
See routes:
routes.MapRoute(
name: "Articles",
url: "Article/{*articleName}",
defaults: new { controller = "Article", action = "Article", id= UrlParameter.Optional }
,
constraints: new { therest = #"\w+" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller="Home", action = "Index", id = UrlParameter.Optional }
);
See ArticleController methods:
public ActionResult Index()
{
...
}
public ActionResult Article(string articleName)
{
...
}
You can add a catch-all parameter to the route like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{*therest}",
defaults: new { controller = "Home", action = "Index" }
);
Notice the asterisk? This marks therest as a "catch-all" parameter, which will match all remaining segments in the URL.
In your action, you would have
public ActionResult Article(string therest)
{
/*...*/
}
This works even for URLs like "Home/Article/This/Is/The/Rest", in which case therest will have the value "This/Is/The/Rest".
If you want to leave out the controller part of the URL completely, you would have
routes.MapRoute(
name: "Default",
url: "Article/{*therest}",
defaults: new { controller = "Home", action = "Index" }
);
which will match URLs like "Article/ThisIs/JustSomeText".
If you want therest to at least contain something, you might add a routing constraint:
routes.MapRoute(
name: "Default",
url: "Article/{*therest}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { therest = #"\w+" }
);
The constraint is a regular expression that therest must match for the route to match.
Stephen Walther has a nice article on routing and catch-all parameters.
Stephen Walther, again, has an article on routing constraints here.
If you are using standard routing change parameter name from someText to id. Otherwise you have to create custom routing for this parameter
You need to define a route in order to use the url you mentioned. For the latest MVC4, routes file exists in this directory App_Start/RouteConfig.cs
add this new route about the the default route.
routes.MapRoute(
name: "Custom",
url: "Article/{someText}",
defaults: new { controller = "Article", action = "Index" }
);
Try loading your url now. It should work now.

Custom route MVC4 with constraints

I have an MVC 4 application that i want to create custom route to respective controllers.
The goal is simply to not show the action name in the url, rather only show controller name and parameters.
routes.MapRoute(
"Account", // Route name
"Account/{param}", // URL with parameters
new { controller = "Account", action = "Account", param = UrlParameter.Optional }, // Parameter defaults
new { controller = #"^(Account)$" } //Parameter constraints
);
routes.MapRoute(
"Login", // Route name
"Login/{returnurl}", // URL with parameters
new { controller = "Login", action = "Login", returnurl = UrlParameter.Optional }, // Parameter defaults
new { controller = #"^(Login)$" } //Parameter constraints
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
At the moment i'm using this route above which work fine until i call another action that is not specified in the default parameter/url of each root.
E.g the route to Login/Login or Login/Login?somevalue works as expected to simply not show the action in the url Logn or Login/somevalue. But as soon another action in the same controller is requested the route resolves to Login/Login/Logout for instance. Why is this occurring when i have set the route to match all requests to Login controller?
All actions in the controller have the returnurl parameter
An explanation or clarification on this matter would be greatly appreciated if some could take time and view my routes.
Any help or thoughts would be nice.
Thanks in advance.

Routing in ASP.NET MVC 4

I have an ASP.NET MVC 4 app. I'm trying to setup my routes in the RouteConfig.cs file. Essentially, I have two URLs I'm currently interested in. Those URLs are:
/App
/App/Auth
My file structure looks like the following:
/Views
App
Index.cshtml
Auth
Index.cshtml
Docs
Index.cshtml
Index.cshtml
In my RouteConfig.cs file, I have the following:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Index",
"Index",
new { controller = "Views", action = "Index" }
);
routes.MapRoute(
"Dashboard", // Route name
"{controller}/auth/{action}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Oddly, when I visit /App/Auth it takes me to /App. For the life of me, I can't figure out why. Does anyone know how to do this? Thanks!
The app is doing exactly what you asked. Your first route goes to controller "App" and action "Index". Move the folder Auth to same level as App and then change your route to:
routes.MapRoute(
"Dashboard", // Route name
"app/auth/{action}/{id}", // URL with parameters
new { controller = "Auth", action = "Index", id = UrlParameter.Optional }
);
An even better solution, forget the Auth controller and just create a view auth.cshtml in the App folder and an action "Auth" in the App controller, and then you can delete the route altogether and /app/auth will get handled by the default route.
Change the route to this:
routes.MapRoute(
"Dashboard", // Route name
"App/auth/{action}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = UrlParameter.Optional }
);