MVC Areas Routing - c#

I have an MVC Application as follows:
Application
Controllers
HomeController
ActionResult Index();
Areas
IT
Controllers
ITHomeController
ActionResult Index();
I have setup the links to work correctly between the main application and the IT Area.
In the URL the link to the IT area shows up as http://localhost/IT/ITHome.
The code for the link is:
#Html.ActionLink("IT Page","Index","ITHome", new { area = "IT" }, null)
How would I have the url simply be http://localhost/IT I'm sure it's simple.
Thanks!

Look for /Areas/IT/ITAreasRegistration.cs
Add a route mapping in its public override void RegisterArea(AreaRegistrationContext context) method, preferably at the very top or at least before the default route for the area.
Something like:
context.MapRoute(
"default_IT",
"IT",
new { action = "Index", controller = "ITHome" },
new[] { "YourAppNamespaceHere.Areas.IT.Controllers" }
);
As for the link... I'd just hand code it to IT Home and be done with it unless you are hosting this as a sub application from the wwwroot, you should be fine.
In this way you can keep your area routes separate from your normal routes.

Renaming the ITHomeController to HomeController may give you ambiguous matching.
You could change the registered route mapping e.g. add a new route that has ITHOME as the default controller if the area matches IT.
routes.MapRoute(
"ITHome",
"IT/{controller}/{action}/{id}",
new { area = "IT", controller = "ITHome", action = "Index", id = UrlParameter.Optional }
);
Then, just for the IT area, it will allow the default controller to be your ITHomeController.

Related

How to mask URL for a C# MVC site with multiple tenant sub-domains

I have a .NET MVC site with multiple tenants all with their own sub-domains. And I'm trying to mask the controller location for a certain subset of those tenants as they're segmented to an area of the site.
For example I want subdomain.domain.com to point to /Controller/View without the extra stuff on the end of the domain.
More specifically,
http://subdomain.domain.com/Controller/View
Becomes:
http://subdomain.domain.com
What's the best way to accomplish this? In the Web.config?
you can use this in global.asax to remove controller and view part from url.
routes.MapRoute("SpecificRoute", "/{id}", new {controller = "YourController", action = "YourAction", id = UrlParameter.Optional});
// default route
routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional} );
Also you can use Route() over your action
[Route("")]
public ActionResult YourAction()
{
...
}
Url will be like http://example.com/

If Route doesn't exist -> go to other route using its default action?

So I'm having a little problem here with routing.
There are two parts to this web application:
  1. Brochure / Display Website
  2. Internal Site / Client Application
We wanted a way to release changes for the brochure without having to do a whole release of said Web application.
Visiting existing named views will take the user to a brochure page, however if it doesn't exist, it will act like they are a client and will redirect them to their company's login screen.
Global.asax:
//if view doesnt exist then url is a client and should be redirected
routes.MapRoute(
name: "Brochure",
url: "{id}",
defaults: new { controller = "brochure", action = "Brochure", id = "Index" },
namespaces: new[] { "Web.Areas.Brochure.Controllers" }
);
//This is home page
routes.MapRoute(
name: "HomeDefault",
url: "{client}/{action}",
defaults: new { controller = "home", action = "index" },
namespaces: new string[] { "Web.Controllers" }
);
Controller:
/// <summary> Check if the view exists in our brochure list </summary>
private bool ViewExists(string name) {
ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
return (result.View != null);
}
/// <summary> Generic action result routing for all pages.
/// If the view doesn't exist in the brochure area, then redirect to interal web
/// This way, even when we add new pages to the brochure, there is no need to re-compile & release the whole Web project. </summary>
public ActionResult Brochure(string id) {
if (ViewExists(id)) {
return View(id);
}
return RedirectToRoute("HomeDefault", new { client = id });
}
This code works fine up until we log in and go to the landing page. It seems to keep the Brochure action in the route and doesn't want to go to the subsequent controller which results in a 500 error.
e.g. 'domain/client/Brochure' when it needs to be: 'domain/client/Index'
Things tried but not worked:
Changing RedirectToRoute() to a RedirectToAction() - this results in a
finite loop of going back to the ActionResult Brochure(). So
changing controllers through that didn't work.
Create an ActionResult called Brochure() inside the 'HomeController'. It
doesn't even get hit.
Passed in namespaces for RedirectToRoute() as an attribute. I knew this would probably not work, but it was worth a try.
So the question is:
How can I get the route to act properly?
If you can restrict id to some subset of all values you can add that constraints to route (i.e. numbers only) to let default handle the rest.
routes.MapRoute(
name: "Brochure",
url: "{id}",
defaults: new { controller = "brochure", action = "Brochure", id = "Index" },
namespaces: new[] { "Web.Areas.Brochure.Controllers" }
constraints : new { category = #"\d+"}
);
If you can't statically determine restrictions - automatically redirecting in your BrochureController similar to your current code would work. The only problem with sample in the question is it hits the same route again and goes into infinite redirect loop - redirect to Url that does not match first rule:
// may need to remove defaults from second route
return RedirectToRoute("HomeDefault", new { client = id, action = "index" });
If standard constraints do not work and you must keep single segment in url - use custom constraints - implement IRouteConstraint and use it in first route. See Creating custom constraints.
There are several issues with your configuration. I can explain what is wrong with it, but I am not sure I can set you on the right track because you didn't provide the all of the URLs (at least not all of them from what I can tell).
Issues
Your Brouchure route, which has 1 optional URL segment named {id}, will match any URL that is 0 or 1 segments (such as / and /client). The fact that it matches your home page (and you have another route that is named HomeDefault that will never be given the chance to match the home page) leads me to believe this wasn't intended. You can make the {id} value required by removing the default value id = "Index".
The Brouchure route has a namespace that indicates it is probably in an Area. To properly register the area, you have to make the last line of that route ).DataTokens["area"] = "Brochure"; or alternatively put it into the /Areas/Brouchure/AreaRegistration.cs file, which will do that for you.
The only way to get to the HomeDefault route is to supply a 2 segment URL (such as /client/Index, which will take you to the Index method on the HomeController). The example URLs you have provided have 3 segments. Neither of the routes you have provided will match a URL with 3 segments, so if these URLs are not getting 404 errors they are obviously matching a route that you haven't provided in your question. In other words, you are looking for the problem in the wrong place.
If you provide your entire route configuration including all Area routes and AttributeRouting routes (including the line that registers them), as well as a complete description of what URL should go to what action method, I am sure you will get more helpful answers.
So the question is:
How can I get the route to act properly?
Unknown. Until you describe what properly is.
Related: Why map special routes first before common routes in asp.net mvc?
Two ways I could have solved this issue:
Way 1
I reviewed the redirect and just passed in an action in order to get a route that has 2 segments in the url. i.e. client/Index. The Index action now handles logins - going past a custom controller.
public class HomeController : CustomController
public ActionResult Brochure(string id, string action) {
if (ViewExists(id)) {
return View(id);
}
return RedirectToAction("Index", "Home", new { client = id, action = "Index" });
}
Way 2
(from #Alexei_Levenkov)
Create a custom Route constraint so the route will be ignored if the view cannot be found.
namespace Web.Contraints {
public class BrochureConstraint : IRouteConstraint {
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
//Create our 'fake' controllerContext as we cannot access ControllerContext here
HttpContextWrapper context = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "brochure");
ControllerContext controllerContext = new ControllerContext(new RequestContext(context, routeData), new BrochureController());
//Check if our view exists in the folder -> if so the route is valid - return true
ViewEngineResult result = ViewEngines.Engines.FindView(controllerContext, "~/Areas/Brochure/Views/Brochure/" + values["id"] + ".cshtml", null);
return result.View != null;
}
}
}
namespace Web {
public class MvcApplication : System.Web.HttpApplication {
//If view doesnt exist then url is a client so use the 'HomeDefault' route below
routes.MapRoute(
name: "Brochure",
url: "{id}",
defaults: new { controller = "brochure", action = "Brochure", id = "Index" },
namespaces: new[] { "Web.Areas.Brochure.Controllers" },
constraints: new { isBrochure = new BrochureConstraint() }
);
//This is home page for client
routes.MapRoute(
name: "HomeDefault",
url: "{client}/{action}",
defaults: new { controller = "home", action = "index" },
namespaces: new string[] { "Web.Controllers" }
);
}
}
I hope this helps someone else out there.

Parameter only route always being triggered MVC3

I have two very simple routes
routes.MapRoute(
"post", // Route name
postPage + "/{slug}", // URL with parameters
new { controller = "Home", action = "Article" } // Parameter defaults
);
routes.MapRoute(
"page", // Route name
"{slug}", // URL with parameters
new { controller = "Home", action = "Page", slug = homePage} // Parameter defaults
);
And here is my controller logic
public ActionResult Article(string slug)
{
return View(repo.GetPost(slug));
}
public ActionResult Page(string slug)
{
if (slug.ToLower() == MetaData.PostsPage.ToLower())
return View("listPosts", repo.GetAllPosts());
else
return View("page", repo.GetPage(slug));
}
homePage and postPage are set from value's in the database. Allowing the user to define the default page as well as the page to show posts.
My issue occurs when adding an area named "Admin". I get a controller added to my RouteTable
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
Now when a user Access Admin/Account/Logon the page loads fine, but my debugger still tries to go into the Home controller and the Page action. But the RouteDebugger says it doesn't match the current request. I'm puzzled on how to fix this.
RouteDebugger screenshot: http://i.stack.imgur.com/7cpHm.png
Debugger going into my HomeControler Page AtionResult: http://i.stack.imgur.com/uSJBK.png
Actually the problem is, Area routes are overriding the global routes, to distinguish both the routes set the relevant namespace of area's controller in the context.MapRoute method in adminAreaRegistraton.cs file. i.e.
context.MapRoute(
"admin_default",
"admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
null,
new string[] { "MVCApplication1.Areas.admin.Controllers" }
);
I found out the issue.
I had a favicon.ico set in the main area of my site, but not the Admin area.
So when I went to the Admin area the browser made a request for favicon.ico that got picked up by that route. Thats why my routes looked fine in the RouteDebugger, because they were.
Thanks for the help Kundan!

multiple controllers in Areas and Main site

I have multiple controllers one in Areas
/Areas/Demo/Admin/AdminController
and other in main site
/Controller/Admin/AdminController
Now getting error
Multiple types were found that match the controller named 'Admin'.
How to resolve the issue? Better if I can change something in Areas/Demo as I have to use same Areas on multiple sites.
Define namespace in your route. http://msdn.microsoft.com/en-us/library/dd492682.aspx
For example my area "Admin" is mapped like this:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "MvcBase.Areas.Admin.Controllers" }
);

The view 'Index' or its master could not be found

Today I've started restructuring my mvc project using areas. So I have some problems with it.
I make areas, move views and master pages, make routing, but when I'm trying to view any area page I get "The view 'Login' or its master could not be found."
Any suggestions? I've googled for some time, but didn't found any solution.
My global.asax routing:
routes.MapRoute("Areaname", "Areaname",
"Areaname/{controller}/{action}/{id}",
new { controller = "controllerName", action = "Home", id = UrlParameter.Optional },
new string[] { "path.to.area.controllers.Controllers" });
Area registration routing:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Areaname_default",
"Areaname/{controller}/{action}/{id}",
new { action = "Home", id = UrlParameter.Optional },
new string[] { "path.to.area.controllers.Controllers" }
);
}
It seems to be correct, but views are still unavailable.
Thanks in advance.
When using Areas, make sure you rewrite your action links like this:
Html.ActionLink("Invoicing link", "Index", "CustomerInvoices", new { area = "Invoicing" }, null)
In this example, you are linking to the "Index" action, inside "CustomerInvoices" controller, inside "Invoicing" Area.
Make sure you do the proper modifications to link to the Login action inside your Area!

Categories