Routing in ASP.NET MVC 4 - 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 }
);

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

How to handle Durandal MVC routing?

I have introduced mvc area concept with the durandal
My hierarchy would be
Area.Web
Areas
Blog
Controller
SocialController
Social
View
Task
Controller
View
Scripts
App
Blog
ViewModels
I have route to the area based on my url. For example, localhost/blog
My route woule be:
routes.MapRoute(
name: "blog",
url: "blog/{action}/{id}",
defaults: new { controller = "blog", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Area.Web.Blog.Controllers" }
);
routes.MapRoute(
name: "Task",
url: "Task/{action}/{id}",
defaults: new { controller = "task", action = "Index", id = UrlParameter.Optional},
namespaces: new string[] { "Area.Web.Task.Controllers" }
);
When I try to navigate to localhost/blog, it call the correct controller and render the correctly. when the durandal route happen blog is excluded.So that my route url not able to fetch the controller.
app.setRoot('viewmodels/social/home', 'entrance');
The following route throws 404 exception, since it ignored the blog in the route url (localhost/social/home)
Please let me know, how to resolve this issue. Is it possible to include area name in all the route and app.set.
Firstly IMHO You should reconsider Your project architecture. For example, if you need to optimize this app with Weyland, both SPAs will end up in the same file what does not gives You required separation.
However, instead of having them both under the app folder, you can make separate folders, and give each SPA it's own main.js file.
In such case You would have following project structure:
Area.Web
AppBlog
Services
Views
ViewModels
main.js
AppTask
Services
Views
ViewModels
main.js
Areas
Blog
Controller
View
Task
Controller
View
Scripts
durandal
etc.
Content
etc.
Your View in Blog Area would use follwoing JS import:
<script src="~/Scripts/require.js" data-main="#Url.Content("~/AppBlog/main")"></script>
while Task Area would use
<script src="~/Scripts/require.js" data-main="#Url.Content("~/AppBlog/main")"></script>
In such setup Your routes would look similar to exactly the same as in Your code
EDIT
routes.MapRoute(
name: "blog",
url: "blog/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Area.Web.Blog.Controllers" }
);
routes.MapRoute(
name: "Task",
url: "Task/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional},
namespaces: new string[] { "Area.Web.Task.Controllers" }
);

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.

Asp.net mvc url routing. Change url

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

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.