Gary Kenyon
Gary Kenyon

Reputation: 368

Naming conventions for view pages and setting controller action for view

I am unsure on how I should be naming my View pages, they are all CamelCase.cshtml, that when viewed in the browser look like "http://www.website.com/Home/CamelCase".

When I am building outside of .NET my pages are named like "this-is-not-camel-case.html". How would I go about doing this in my MVC4 project?

If I did go with this then how would I tell the view to look at the relevant controller? Views/Home/camel-case.cshtml

Fake edit: Sorry if this has been asked before, I can't find anything via search or Google. Thanks.

Upvotes: 2

Views: 2179

Answers (1)

Nick Larsen
Nick Larsen

Reputation: 18877

There are a few ways you can do this:

Name all of your views in the style you would like them to show up in the url

This is pretty simple, you just add the ActionName attribute to all of your actions and specify them in the style you would like your url to look like, then rename your CamelCase.cshtml files to camel-case.cshtml files.

Use attribute routing

Along the same lines as above, there is a plugin on nuget to enable attribute routing which lets you specify the full url for each action as an attribute on the action. It has convention attributes to help you out with controller names and such as well. I generally prefer this approach because I like to be very explicit with the routes in my application.

A more framework-y approach

It's probably possible to do something convention based by extending the MVC framework, but it would be a decent amount of work. In order to select the correct action on a controller, you'd need to map the action name on its way in to MVC to its CamelCase equivalent before the framework uses it to locate the action on the controller. The easiest place to do this is in the Route, which is the last thing to happen before the MVC framework takes over the request. You'll also need to convert the other way on the way out so the urls generated look like you want them to.

Since you don't really want to alter the existing method to register routes, it's probably best write a function in application init that loops over all routes after they have been registered and wraps them with your new functionality.

Here is an example route and modifications to application start that achieve what you are trying to do. I'd still go with the route attribute approach however.

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        WrapRoutesWithNamingConvention(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }

    private void WrapRoutesWithNamingConvention(RouteCollection routes)
    {
        var wrappedRoutes = routes.Select(m => new ConventionRoute(m)).ToList();
        routes.Clear();
        wrappedRoutes.ForEach(routes.Add);
    }

    private class ConventionRoute : Route
    {
        private readonly RouteBase baseRoute;

        public ConventionRoute(RouteBase baseRoute)
            : base(null, null)
        {
            this.baseRoute = baseRoute;
        }

        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var baseRouteData = baseRoute.GetRouteData(httpContext);
            if (baseRouteData == null) return null;

            var actionName = baseRouteData.Values["action"] as string;
            var convertedActionName = ConvertHyphensToPascalCase(actionName);
            baseRouteData.Values["action"] = convertedActionName;
            return baseRouteData;
        }

        private string ConvertHyphensToPascalCase(string hyphens)
        {
            var capitalParts = hyphens.Split('-').Select(m => m.Substring(0, 1).ToUpper() + m.Substring(1));
            var pascalCase = String.Join("", capitalParts);
            return pascalCase;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            var valuesClone = new RouteValueDictionary(values);
            var pascalAction = valuesClone["action"] as string;
            var hyphens = ConvertPascalCaseToHyphens(pascalAction);
            valuesClone["action"] = hyphens;
            var baseRouteVirtualPath = baseRoute.GetVirtualPath(requestContext, valuesClone);
            return baseRouteVirtualPath;
        }

        private string ConvertPascalCaseToHyphens(string pascal)
        {
            var pascalParts = new List<string>();
            var currentPart = new StringBuilder();
            foreach (var character in pascal)
            {
                if (char.IsUpper(character) && currentPart.Length > 0)
                {
                    pascalParts.Add(currentPart.ToString());
                    currentPart.Clear();
                }

                currentPart.Append(character);
            }

            if (currentPart.Length > 0)
            {
                pascalParts.Add(currentPart.ToString());
            }

            var lowers = pascalParts.Select(m => m.ToLower());
            var hyphens = String.Join("-", lowers);
            return hyphens;
        }
    }
}

Upvotes: 3

Related Questions