ASP.NET MVC 4 Core routing - asp.net-mvc

I have set up the following structure for my website. (see link below)
http://postimg.org/image/ut4klihrp/
localhost:62540 Goes to the index page of the core
localhost:62540/www/Home Goes to the index page of the WWW
localhost:62540/cms/Home Goes to the indec page of the cms
I basically want the 'default' route (localhost:62540) to go to my WWW project. How can i do this or does anybody know a tutorial were the principle for this get explained? Or is this not possible since i use the area method.
Eventually i want to remove the view and controller from the core project.
Route config in www :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);

You can specify the default area in the defaults:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "www", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);

Related

Remove home directory from the URL with ASP.NET MVC

Situation
I'll build my URL like follow:
The overview page on / (at HomeController).
The detail page on /details/123 instead of /home/details/123 knowing that 123 always is a number (at HomeController).
An info page on /info instead of /home/info (at HomeController).
The logon page on /account/signin (at AccountController).
I've don't have more pages on my application.
Try 1
I've created two routes like follow:
routes.MapRoute(
name: "Home",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem is that the account page gives a 404. The other pages works.
Try 2
I've created one route like below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
On each method inside the HomeController added a the Route attribute with the correct URL.
The problem is now that the overview and logon pages works the others gives now a 404.
Question
How could I remove the home directory from the URL with ASP.NET MVC?
To use attribute routes you need to add
routes.MapMvcAttributeRoutes();
to your RouteConfig.cs file.
It should go before your default route.

Add always constraint to route

I'm new to ASP.Net Mvc and I'm so confused about routing configurations. After I deployed my project to production requirements changed about urls. First version of my application my routing configuration was like below
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
But customer wanted to access application like "http://{serveraddress}/ClaimFilePortal/{controller}/{action}/{id}". After this I have changed my routing config like below.
routes.MapRoute(
name: "Default",
url: "{applicationname}/{controller}/{action}/{id}",
defaults: new { applicationname ="ClaimFilePortal",controller = "Account", action = "Index", id = UrlParameter.Optional },
constraints:new {applicationname = "ClaimFilePortal"}
);
But in controllers' code there are redirections like and mvc not adding "ClaimFilePortal" constraint.
return RedirectToAction("Index2", "Account",
new RouteValueDictionary(new { systemUserId = systemUserId, returnUrl = "/Upload/RedirectToFolder" + Request.Url.Query}));
So, how can I add a configuration for all redirections (Redirect(), RedirectToAction(), RedirectToRoute() etc.) to add my constraints before all urls.
To add 'ClaimFilePortal' as default prefix, remove the {} from it and try like:
routes.MapRoute(
name: "Default",
url: "applicationname/{controller}/{action}/{id}",
defaults: new { applicationname ="ClaimFilePortal",controller = "Account", action = "Index", id = UrlParameter.Optional },
constraints:new {applicationname = "ClaimFilePortal"}
);

how can limit the action the action in specific namespace?

I have create a solution, and add the four projects into it.
that contains three mvc site( SiteA,SiteB,SiteC)
and a common core class library(name is core project).
now all of the mvc sites's Controllers was moved to the core project.
and I reference the core project to these mvc sites.
I try to register routes in these sites.
SiteA:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "AWebDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.AWeb.Controllers" }
);
SiteB:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "BWebDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.BWeb.Controllers" }
);
SiteC:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CWebDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.CWeb.Controllers" }
);
the question is :
when i visit the siteA, and how to prevent the user to vistit the siteB controller or SiteC Controller?
I want to the controller only can be found in the special namespace. can not search in other namespace. so how to solve it?
You can create action filter or custom action filter and use it before actions or controllers. for this store namespace name and check in filter. if namespace of target action is equal by namespace name then allow access. Another solution is to use Area. To generate a link to a different area, you must explicitly pass the target area name in the routeValues parameter for these methods (Linking Between Areas).

MVC custom routing setup

I'm new to MVC. I've got my default routing setup as :
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);
That works beautifully.
I'd like to setup the routing to also accommodate the following path :
Apps/DemoApp1 ">>controllers>>models>>views under Demo1"
So that I'd be able to add a folder called DemoApp2 under Apps and still have it work the same. The folder Apps must act like an Area if that makes sense.

ASP.NET MVC routing to action

I'm having trouble doing what I want to achieve when doing routing in ASP.NET MVC.
What I want to do is the following:
When typing http://localhost/MyWebsite/, I want to redirect to http://localhost/MyWebsite/Login/ (The redirection to the action Index is implied here)
When typing http://localhost/MyWebsite/MyAction, I want to redirect to http://localhost/MyWebsite/Login/MyAction
Point 1 was achieved using the following lines in the file RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
But I cannot accomplish point 2. Note that the user does not come from another action in a controller, but actually types the address in his browser.
Thanks for your help.
since /MyWebsite/Login and /MyWebsite/MyAction both have two segments in the URL, they're both matching your defined Route.
You can use a Route Constraint to only match /MyWebsite/Login to your first point, followed by a modified second route mapping:
routes.MapRoute(
name: "Default",
url: "MyWebsite/Login/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Actions",
url: "MyWebsite/{action}/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });

Resources