0

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?

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • 4
    Your route expects `id` then `slug`. It seems like you intend to only pass the `slug`. Try changing the url in the route config to: "article/{slug}" – maccettura Mar 27 '17 at 18:20
  • 1
    You should also avoid the "catch-all" (`*`). That's only necessary if you need to capture slashes as part of the slug, as well. – Chris Pratt Mar 27 '17 at 18:23
  • 2
    Keep in mind routes are evaluated in a FIFO order. Make sure your route is listed before any generic routes that would potentially conflict. Also, I think the `/{id}` section should be removed to get the URL you want. – Berin Loritsch Mar 27 '17 at 18:23
  • You should probably look into using URL Rewrite on your server to accomplish this. – Travis J Mar 27 '17 at 18:38
  • If you look at stack overflow's URL you would see that they are using a similar format for their pages. Do not include any dots (`.`) in the slug as it would be interpreted as file extensions and can cause routing issues. – Nkosi Mar 27 '17 at 19:46

1 Answers1

1

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

Community
  • 1
  • 1
Andy T
  • 10,223
  • 5
  • 53
  • 95