1

I am trying to do some MVC routing, following online tutorials, but for some reason the routing is not working.

I am trying to do http://www.website.com/news/news-title

The route I am trying to do is below.

routes.MapRoute(
    "News",
    "{controller}/{url}",
    new { controller = "News", action = "Index", url = "" }
);

Within my NewsController I have the following ActionResult.

public ActionResult Index(String url)
{
    return View();
}

However, when stepping thorough the code, url is not being populated.

Thanks

== Update ==

Thank you all for your replies.

I have no amended the Route to below

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.LowercaseUrls = true;

        routes.Add(new SubdomainRoute());

        routes.MapRoute(
            "News",
            "News/{action}/{slug}",
            new { controller = "News", action = "Index" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

This URL works: /news/index/?slug=test-news-title

But this URL does not: /news/index/test-news-title

=== Further Edit ===

It appears my subdomain route is messing with it. If i remove the subdomain route it works fine.

Thanks.

2
  • slug would be a good name! Commented Mar 18, 2016 at 12:58
  • So, could you post your SubDomain route too? Make sure it returns null in the case where it doesn't match the incoming request, or the routing framework will not check any of your other routes. The first match always wins. Commented Mar 18, 2016 at 14:50

3 Answers 3

1

Most likely, your route is too broad. But it depends on how the rest of your routes are configured. You would need to post your entire route configuration (including area routes and attribute routing) in order to reasonably get an answer what is wrong with your route.

However, you can be more specific with this route because you know that it needs to start with /News.

routes.MapRoute(
    "News",
    "News/{url}",
    new { controller = "News", action = "Index" }
);

Also, it probably doesn't make sense to make the URL parameter optional by providing a default value. If you remove url = "", the url parameter is required in the URL. If configured as above, if you just pass /News, it won't match this route. However, as you have it this URL will match.

Finally, make sure this route is placed in the right order. It should be placed before your default route (if you still have it).

Sign up to request clarification or add additional context in comments.

Comments

0

You have set for the parameter url the empty string. you should use instead UrlParameter.Optional (or removing it at all if is mandatory):

routes.MapRoute(
    "News",
    "{controller}/{url}",
    new { controller = "News", action = "Index", url = UrlParameter.Optional }
);

Comments

0

You are missing the action part in the MapRoute

routes.MapRoute(
    "News",
    "{controller}/{action}/{url}",
    new { controller = "News", action = "Index", url = "" }
);

Hope this help

2 Comments

Thanks, but how do I remove /Index/ from the url?
This answer is not helpful if the OP doesn't want the action in the URL. Providing a default of action = "Index" is enough to make it match the action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.