Parmod Kumar
Parmod Kumar

Reputation: 41

ASP.NET MVC 5 -Custom URLs with Attribute Routing and Dynamic controller and Action methods

NET MVC 5** I want urls like this http://borntoprogram.com/.Net/.Net-Core/

I know i can achieve it using Attribute routing as shown below

[RoutePrefix(".Net")]
[Route("{action=Index}")]
public class NetController : Controller
{
    [Route(".Net-Core")]
    public ActionResult NetCore()
    {
        return View();
    }
}

but i want to Generate Controllers and Actions methods dynamically. And use Attribute routing with that.
I want to use Dots (.) in URL ,
second thing i want the urls to be very simple having just 3 parts like DomainName/CategoryOfArticle/ArticleName as in the URL DomainName -BornToProgram.com , CategoryofArticle -.Net , ArticleName -.Net-Core

IN all I WANT to allow admin to Decide parts of the URL For new articles that he can submit on monthly or daily basis. Like Category of Article (.Net in example) and Then Name of the Article( .Net-Core , .Net-Framework) The ADMIN want full control on the URLs

Upvotes: 4

Views: 2188

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

Since attributes are used to attach metadata to a class, it is not possible to dynamically add them at runtime. But even if you could, it is unlikely the Attribute Routing framework that reads and converts them into Route instances would function correctly.

If you need to make dynamically driven routes, the solution is to inherit RouteBase so you can add URLs and/or specify which controller they should refer to at runtime. You should cache the URL list as in the example, but there is no reason why you couldn't adapt the cache so individual URLs could be added to it one by one when they are added in your application (add them to the cache and the data source in one go, so they are both immediately and permanently available).

Upvotes: 2

Related Questions