MVC routing for CMS pages - asp.net-mvc

I'm creating a cut down version of a CMS using MVC 5 and I'm trying to get through the routing side of things.
I need to handle pages with urls such as /how-it-works/ and /about-us/ etc and therefore content is keyed on these paths.
In my RouteConfig file I'm using a 'catch all' route as follows::
routes.MapRoute("Static page", "{*path}", new { controller = "Content", action = "StaticPage" });
This successfully hits the controller action I'm looking, however it therefore means that requests for controller actions that actually do exist (for example /navigation/main also get sent down this route).
I know that I can have a route that matches /navigation/main however I'd rather configure MVC to do this as default, like it does when I don't add the rule I have above, any ideas?

Add your "catch all" route on top of "default" route and add a route constrain to path like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Static page",
"{*path}",
new { controller = "Content", action = "StaticPage" }
new { path = new PathConstraint() });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
}
PathConstraint should derive from from IRouteConstraint interface and can be something like this:
public class PathConstraint: IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (values[parameterName] != null)
{
var permalink = values[parameterName].ToString();
//gather all possible paths from database
//and check if permalink is any of them
//return true or false
return database.GetPAths().Any(p => p == permalink);
}
return false;
}
}
So if "path" is not one of your pages paths, PathConstrain will not be satisified and "Static page" route will be skiped and pass to next route.

Related

Why map special routes first before common routes in asp.net mvc?

From the www:
...The routing engine will take the first route that matches the supplied URL and attempt to use the route values in that route. Therefore, less common or more specialized routes should be added to the table first, while more general routes should be added later on...
Why should I map specialized routes first? Someone can give me an example please where I can see the failing of "map common route first" ?
The routing engine will take the first route that matches the supplied URL and attempt to use the route values in that route.
The reason why this happens is because the RouteTable is used like a switch-case statement. Picture the following:
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 1:
Console.WriteLine("Second Case 1");
break;
default:
Console.WriteLine("Default case");
break;
}
If caseSwitch is 1, the second block is never reached because the first block catches it.
Route classes follow a similar pattern (in both the GetRouteData and GetVirtualPath methods). They can return 2 states:
A set of route values (or a VirtualPath object in the case of GetVirtualPath). This indicates the route matched the request.
null. This indicates the route did not match the request.
In the first case, MVC uses the route values that are produced by the route to lookup the Action method. In this case, the RouteTable is not analyzed any further.
In the second case, MVC will check the next Route in the RouteTable to see if it matches with the request (the built in behavior matches the URL and constraints, but technically you can match anything in the HTTP request). And once again, that route can return a set of RouteValues or null depending on the result.
If you try to use a switch-case statement as above, the program won't compile. However, if you configure a route that never returns null or returns a RouteValues object in more cases than it should, the program will compile, but will misbehave.
Misconfiguration Example
Here is the classic example that I frequently see posted on StackOverflow (or some variant of it):
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomRoute",
url: "{segment1}/{action}/{id}",
defaults: new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
In this example:
CustomRoute will match any URL that is either 1, 2, or 3 segments in length (note that segment1 is required because it has no default value).
Default will match any URL that is 0, 1, 2, or 3 segments in length.
Therefore, if the application is passed the URL \Home\About, the CustomRoute will match, and supply the following RouteValues to MVC:
segment1 = "Home"
controller = "MyController"
action = "About"
id = {}
This will make MVC look for an action named About on a controller named MyControllerController, which will fail if it doesn't exist. The Default route is an unreachable execution path in this case because even though it will match a 2-segment URL, the framework will not give it the opportunity to because the first match wins.
Fixing the Configuration
There are several options on how to proceed to fix the configuration. But all of them depend on the behavior that the first match wins and then routing won't look any further.
Option 1: Add one or more Literal Segments
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomRoute",
url: "Custom/{action}/{id}",
// Note, leaving `action` and `id` out of the defaults
// makes them required, so the URL will only match if 3
// segments are supplied begining with Custom or custom.
// Example: Custom/Details/343
defaults: new { controller = "MyController" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Option 2: Add 1 or more RegEx Constraints
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomRoute",
url: "{segment1}/{action}/{id}",
defaults: new { controller = "MyController", action = "Index", id = UrlParameter.Optional },
constraints: new { segment1 = #"house|car|bus" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Option 3: Add 1 or more Custom Constraints
public class CorrectDateConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var year = values["year"] as string;
var month = values["month"] as string;
var day = values["day"] as string;
DateTime theDate;
return DateTime.TryParse(year + "-" + month + "-" + day, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out theDate);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomRoute",
url: "{year}/{month}/{day}/{article}",
defaults: new { controller = "News", action = "ArticleDetails" },
constraints: new { year = new CorrectDateConstraint() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Option 4: Make Required Segments + Make the Number of Segments not Match Existing Routes
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomRoute",
url: "{segment1}/{segment2}/{action}/{id}",
defaults: new { controller = "MyController" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
In the above case, the CustomRoute will only match a URL with 4 segments (note these can be any values). The Default route as before only matches URLs with 0, 1, 2, or 3 segments. Therefore there is no unreachable execution path.
Option 5: Implement RouteBase (or Route) for Custom Behavior
Anything that routing doesn't support out of the box (such as matching on a specific domain or subdomain) can be done by implementing your own RouteBase subclass or Route subclass. It is also the best way to understand how/why routing works the way it does.
public class SubdomainRoute : Route
{
public SubdomainRoute(string url) : base(url, new MvcRouteHandler()) {}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
if (routeData == null) return null; // Only look at the subdomain if this route matches in the first place.
string subdomain = httpContext.Request.Params["subdomain"]; // A subdomain specified as a query parameter takes precedence over the hostname.
if (subdomain == null) {
string host = httpContext.Request.Headers["Host"];
int index = host.IndexOf('.');
if (index >= 0)
subdomain = host.Substring(0, index);
}
if (subdomain != null)
routeData.Values["subdomain"] = subdomain;
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
object subdomainParam = requestContext.HttpContext.Request.Params["subdomain"];
if (subdomainParam != null)
values["subdomain"] = subdomainParam;
return base.GetVirtualPath(requestContext, values);
}
}
This class was borrowed from: Is it possible to make an ASP.NET MVC route based on a subdomain?
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new SubdomainRoute(url: "somewhere/unique"));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
NOTE: The real gotcha here is that most people assume that their routes should all look like the Default route. Copy, paste, done, right? Wrong.
There are 2 problems that commonly arise with this approach:
Pretty much every other route should have at least one literal segment (or a constraint if you are into that sort of thing).
The most logical behavior is usually to make the rest of the routes have required segments.
Another common misconception is that optional segments mean you can leave out any segment, but in reality you can only leave off the right-most segment or segments.
Microsoft succeeded in making routing convention-based, extensible, and powerful. They failed in making it intuitive to understand. Virtually everyone fails the first time they try it (I know I did!). Fortunately, once you understand how it works it is not very difficult.

ASP.NET MVC 4 route username / action issue

I am currently working on an asp.net mvc 4 application and I have the need for the following type of urls:
Urls that need to be routed
http://www.mysite.com/foo/user1 <------- {username}
http://www.mysite.com/foo/edit
http://www.mysite.com/foo/delete/1
http://www.mysite.com/bar/user1 <------- {username}
http://www.mysite.com/bar/edit
http://www.mysite.com/bar/delete/1
The issue I'm having is that currently {username} gets treated as an action so to work around the problem I implemented the following routes, but this would mean that every time I want to implement a new action, or have a controller that needs {username}, I would have to update my routes:
Only Foo routes shown
routes.MapRoute("FooSomeAction", "foo/someaction", new { controller = "Food", action = "SomeAction" });
routes.MapRoute("FooDelete", "foo/delete/{id}", new { controller = "Food", action = "Delete" });
routes.MapRoute(
"FooProfile",
"foo/{username}",
new { controller = "Foo", action = "Index", username = "" }
);
// Default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
2 Questions
1) Is there any way I can achieve the above urls without hardcoding all the routes?
2) What is the best way to handle a situation where someone uses a username that happens to be the same name as a controller or action name?
DotnetShadow
You could create a custom route constraint that would check if the username exists in the possible actions for the controller. If it finds an action match, it fails and will use your default route (Edit for example). You may want to cache the list for performance reasons, but I leave that up to you.
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
public static List<string> GetActionNames(string controllerName)
{
controllerName = controllerName + "Controller";
var controller = GetSubClasses<Controller>().FirstOrDefault(c => c.Name == controllerName);
var names = new List<string>();
if (controller != null)
{
var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (var info in methods)
{
if (info.ReturnType == typeof(ActionResult))
{
names.Add(info.Name);
}
}
}
return names;
}
public class UsernameNotAction : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
int i = 0;
var username = values["username"];
var actionList = GetActionNames(values["controller"].ToString());
return !actionList.Any(a => a.ToUpper() == username.ToString().ToUpper());
}
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"FooProfile",
"{controller}/{username}",
new { controller = "Home", action = "Index2", username = "" },
new { IsParameterAction = new UsernameNotAction() }
);
// Default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This isn't really the answer you're looking for, sorry.
1) There's no way to route that way. There's nothing to differentiate those routes from one another, other than what you've done. I have to question why this is even necessary, I'm sure you have a good reason, but it makes no sense to me. You're still using the Index action, so why not just /foo/index/username. All I can come up with, is you have no control over the url for some reason.
2) If you use the default route, there's no problem. With your routing, problem. Your only real option is to make your controller and action names reserved words (prevent users from being created with those usernames in the database).
Sorry I couldn't really help you.
You can't do it like that unless you route every single route and that is not the best way to go.
What's so wrong in having the Action name in it?

Asp.Net Custom Routing and custom routing and add category before controller

I'm just learning MVC and want to add some custom routing to my site.
My site is split into brands so before accessing other parts of the site the user will select a brand. Rather than storing the chosen brand somewhere or passing it as a parameter I would like to make it part of the URL so to access the NewsControllers index action for example rather than "mysite.com/news" I would like to use "mysite.com/brand/news/".
I just really want to add a route which says if a URL has a brand, go to the controller/action as normal and pass through the brand...is this possible?
Thanks
C
Yes, this is possible. First, you must create a RouteConstraint to insure that a brand has been chosen. If a brand has not been chosen, this route should fail, and a route to an action to redirect to the brand selector should follow. The RouteConstraint should look like this:
using System;
using System.Web;
using System.Web.Routing;
namespace Examples.Extensions
{
public class MustBeBrand : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
// return true if this is a valid brand
var _db = new BrandDbContext();
return _db.Brands.FirstOrDefault(x => x.BrandName.ToLowerInvariant() ==
values[parameterName].ToString().ToLowerInvariant()) != null;
}
}
}
Then, define your Routes as follows (assuming that your brand selector is the home page):
routes.MapRoute(
"BrandRoute",
"{controller}/{brand}/{action}/{id}",
new { controller = "News", action = "Index", id = UrlParameter.Optional },
new { brand = new MustBeBrand() }
);
routes.MapRoute(
"Default",
"",
new { controller = "Selector", action = "Index" }
);
routes.MapRoute(
"NotBrandRoute",
"{*ignoreThis}",
new { controller = "Selector", action = "Redirect" }
);
Then, in your SelectorController:
public ActionResult Redirect()
{
return RedirectToAction("Index");
}
public ActionResult Index()
{
// brand selector action
}
If your home page is not the brand selector, or there is other non-brand content on the site, then this routing is not correct. You will need additional routes between BrandRoute and Default which match routes to your other content.

ASP.NET MVC3 Routing various subfolders to the same controller

I'm trying to set up my MVC project to have URLs so that I can go to:
/Groups/
/Groups/Register
/Groups/Whatever
But in my controller, I can also flag some actions as admin only, so that they are accessed at:
/Admin/Groups/Delete/{id}
I would like to keep one GroupController, and have actions so that:
public class GroupController : Controller
{
public ActionResult Index(){
return View();
}
[AdminAction]
public ActionResult Delete(int id){
...
return View();
}
}
Allows:
/Groups is a valid URL.
/Admin/Groups is a valid URL (but would call some other action besides Index - maybe)
/Admin/Groups/Delete/{id} is a valid URL (post only, whatever)
/Groups/Delete is an INVALID url.
I realize this is probably a pretty broad question, but I'm new to MVC and I'm not really sure where to start looking, so if you could just point me in the right direction that would be hugely appreciated.
As we discussed in the comments below, while it is possible to use my original answer below to achieve the routing solution you requested, a better solution is to use Areas, establish an Admin area, and create controllers in your Admin area to handle the administrative tasks for different objects, such as Group, User, etc. This allows you to set up restricted administrative functions more easily, and is both a better design and a better security model.
ORIGINAL ANSWER
What you want can be accomplished by using the following routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Admin", // Route name
"admin/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
However, as Akos said in the comments, it is a much better design to separate the administrative functions into a different controller. While this is possible, I would recommend against using this design.
UPDATE
It is possible to use a RouteConstraint on your Default route to make it fail if Admin actions are requested. The Default route would look like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, // Parameter defaults
new { action = IsNotAdminAction() } // route constraint
);
The RouteConstraint would look like this:
public class IsNotAdminAction : IRouteConstraint
{
private string adminActions = "create~delete~edit";
public IsNotAdminAction()
{ }
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
// return false if there is a match
return !adminActions.Contains(values[parameterName].ToString().ToLowerInvariant());
}
}

How do I configure ASP.NET MVC routing to hide the controller name on a "home" page?

Following on from this question:
ASP.NET MVC Routing with Default Controller
I have a similar requirement where my end user doesn't want to see the controller name in the url for the landing or "home page" for their application.
I have a controller called DeviceController which I want to be the "home page" controller. This controller has a number of actions and I'd like to use URL's like the following:
http://example.com -> calls Index()
http://example.com/showdevice/1234 -> calls ShowDevice(int id)
http://example.com/showhistory/1224 -> calls ShowHistory(int id)
I also need links generated for this controller to leave out the /device part of the url.
I also have a number of other controllers, for example BuildController:
http://example.com/build
http://example.com/build/status/1234
http://example.com/build/restart/1234
and so on. The URL's for these controllers are fine as they are.
The problem is that I just can't seem to get my head around the routing for this even after studying the answers to the question referenced above.
Can someone provide a code sample explaining how to do this?
I'm using ASP.NET MVC2.
Try this:
private void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("default", "{controller}/{action}/{id}",
new { action = "index", id = "" },
// Register below the name of all the other controllers
new { controller = #"^(account|support)$" });
routes.MapRoute("home", "{action}",
new { controller = "device", action = "index" });
}
e.g. /foo
If foo is not a controller then it's treated as an action of the device controller.
Step 1:
Create the route constraint.
public class RootRouteConstraint<T> : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
return rootMethodNames.Contains(values["action"].ToString().ToLower());
}
}
Step 2:
Add a new route mapping above your default mapping that uses the route constraint that we just created. The generic parameter should be the controller class you plan to use as your “Root” controller.
routes.MapRoute(
"Root",
"{action}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional},
new {isMethodInHomeController = new RootRouteConstraint<HomeController>()}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{controller = "Home", action = "Index", id = UrlParameter.Optional}
);
Now you should be able to access your home controller methods like so:
example.com/about,
example.com/contact
This will only affects the url of HomeController. Alll other Controllers will have the default routing functionality.

Resources