0

I have Controller Blog

I have Action Index() and Show(stirng id)

Index() displaying all posts and Show(sting id) displaying single post

I want to map Blog/Show/id to responce on Blog/id

So i went to Global.asxc and did that:

routes.MapRouteLowercase(
                "Blog", 
                "Blog/{id}", 
                new { controller = "Blog", action = "Show", id = UrlParameter.Optional }
            );

But it is seams not to be worked, may be some one may help?

0

2 Answers 2

1

You could have the following routes:

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

    routes.MapRoute(
        "Blog", 
        "Blog/{id}", 
        new { controller = "Blog", action = "Show" }
    );

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
    );
}

Now:

  • /Blog/123 will map to BlogController/Show(123) action
  • / and /Blog will map to BlogController/Index action
Sign up to request clarification or add additional context in comments.

4 Comments

I already have default route routes.MapRouteLowercase( Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
@jason, in the correct order? Then everything should work. What is this MapRouteLowercase? It is not ASP.NET MVC standard. Something you have written? Given its name I suspect that it requires all your routes to be lowercase, so you cannot have Blog/id, you might need blog/id.
i did some changes and i post another question here stackoverflow.com/questions/7283622/… may be you may have look, i decided not to use Show() action but get some troubles
@jason, so maybe you could delete this question if it no longer applies or accept some answer if it helped you solve the original issue.
0

The new route that you want to create should be the first one registered, otherwise the default routing will kick in and process the request.

Ensure that

routes.MapRouteLowercase(
                "Blog", 
                "Blog/{id}", 
                new { controller = "Blog", action = "Show", id = UrlParameter.Optional }
            );

is placed before

 routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

in Global.asax RegisterRoutes

Comments

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.