Ortund
Ortund

Reputation: 8245

How to make MVC route to a more user friendly url?

EDIT

The 404 error persists even after having corrected the route to match the Action per below.

EDIT 2

The URL I'm trying to reach has a . in it which I think is causing problems since the other article URL doesn't have one and works fine. How do I url encode this out?


I'm normally totally fine with /site/contoller/action/param type urls

For my blog site I want something different though.

Here's an example of an existing link for one of my blog articles:

/Article/tweaking-asp.net-identity-to-add-first-and-last-name-as-username

I have the Index View on the Article Controller accepting a param called slug:

public ActionResult Index(string slug)
{
    ApplicationDbContext Context = new ApplicationDbContext();

    var Article = Context.Articles.FirstOrDefault(x => x.Slug == slug);

    // get and list articles and build the ViewModel

    return View(Model);
}

I updated my RouteConfig.cs to handle this, but I seem to have botched it as I get an HTTP404 error when I try to navigate to an article with this config.

routes.MapRoute(
    name: "Articles",
    url: "Article/{slug}",
    defaults: new { controller = "Article", action = "Index", slug = UrlParameter.Optional }
);

How can I route correctly to this type of URL?

Upvotes: 0

Views: 823

Answers (1)

Andy T
Andy T

Reputation: 9881

Based on the provided slug (tweaking-asp.net-identity-to-add-first-and-last-name-as-username), I believe your problem is caused by the period in "asp.net". Based on other answers, it seems IIS thinks you are requesting a file and it looks for it, since it doesn't find it, it is returning a 404.

Please try it with a slug that does not have a dot in it, if that does work, then this is probably the case, here is a possible solution: Dots in URL causes 404 with ASP.NET mvc and IIS

Upvotes: 1

Related Questions