Skip to content

A set of IRouteConstraint that functions on EnumValues and the name value of the DisplayAttribute on EnumValues for really nice urls

Notifications You must be signed in to change notification settings

zealofzebras/EnumConstraint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

EnumConstraint

A set of IRouteConstraint that functions on EnumValues and the name value of the DisplayAttribute on EnumValues for really nice urls

Installation

Install package from nuget Sustainable.Web.Mvc.EnumConstraint

EnumDisplayNameConstraint

How to have pretty urls with the slug determined by the display name of the enum value.

Having an enum like this

    public enum EventTypeEnum
    {

        [Display(Name = "MyEvent", Description = "Events just for me")]
        SingleEvent,
        [Display(Name = "OurEvents", Description = "Events for us")]
        MultipleEvents,
        [Display(Name = "Everybody", Description = "Events for everybody")]
        All,
    }

and an action on a controller like this

    public async Task<ActionResult> Details(EventTypeEnum type, string id)
    {
        // Notice how the parameter is the enum not a text field that you have to parse
        return View();
    }

It is possible to have urls that look like this

https://sample.com/Events/OurEvents/FirstEventId

By setting the route information like this

    app.UseMvc(routes =>
            {
                routes.MapRoute("events", "Events/{type}/{id}",
                        new { Controller = "Events", Action = "Details" },
                        new { type = new EnumDisplayNameConstraint<Models.Data.EventTypeEnum>(true)} 
                );

                // Default mvc route
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

EnumValueConstraint

How to have pretty urls with the slug determined by the enum value.

TODO

About

A set of IRouteConstraint that functions on EnumValues and the name value of the DisplayAttribute on EnumValues for really nice urls

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages