ASP.NET MVC: Routing custom slugs without affecting performance

后端 未结 2 546
执念已碎
执念已碎 2020-12-28 23:55

I would like to create custom slugs for pages in my CMS, so users can create their own SEO-urls (like Wordpress).

I used to do this in Ruby on Rails and PHP framewor

2条回答
  •  爱一瞬间的悲伤
    2020-12-29 00:12

    I've edited my answer to give a more complete answer to your questions:

    Answer to Question 1:

    Registering routes is initialized on startup. (Perhaps also when the Application Pool recycles, it's highly probable.) I also think there is nothing wrong with your approach since it is occuring only once. I do the same thing querying all supported languages from the database to register them as /TwoLetterISOLanguageName (/nl, /en, /de, etc.).

    Answer to Question 2:

    This should work passing a model: Put it before the Default route!

    routes.MapRoute(
        name: "Contact",
        url: "contact/{action}",
        defaults: new { controller = "Contact", 
                        action = "Index",
                        MyModel = new MyModel { Name = "hello" } });
    

    The ContactController:

    public ActionResult Index(MyModel mymodel)
    {
        return Content(mymodel.Name);
    }
    

    The Model:

    public class MyModel
    {
        public string Name { get; set; }
    }
    

提交回复
热议问题