1

I am trying to make a simple sample of passing string parameter in URL not using the queryString at this example. First of all I added a new MapRouteto RouteConfig.cs file as

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

and in Controller I have

 public class AppController : Controller
    {
        public string Index(string name)
        {
            return HttpUtility.HtmlEncode("Hello " + name );
        }
    }

but the view is not displaying the string parameter. For example a URL like this http://localhost:59013/App/Index/Ali only return Hello!

enter image description here

Why is this happening?

Update

  routes.MapRoute(
      name: "app",
      url: "{controller}/{action}/{name}",
      defaults: new {action = "Index" }
);
    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
5
  • 5
    Change the order of your registered routes. The default has to be the last. But, your app route pattern is wrong and will always collide with the default route. You better change its pattern to url: "App/Index/{name}" (or perhaps to the more friendlier url: "App/{name}") both with defaults: new { controller = "App", action = "Index" }. Commented Sep 21, 2016 at 17:04
  • Thanks Haim770, you were right changing the custom route priority fixed the issue but how can I get rid of the \index in the url? I already add defaults: new {action = "Index" } but I am getting error when I try something like http://localhost:59013/App/Ali/ Commented Sep 21, 2016 at 17:20
  • Please add the modified routes to the question Commented Sep 21, 2016 at 17:21
  • Can you please check the update? Commented Sep 21, 2016 at 17:25
  • See my answer. You're missing the controller = "App" part. Commented Sep 21, 2016 at 17:28

1 Answer 1

2

First, you need to change the order of your registered routes so the default will be the last.

Secondly, your app route pattern is wrong and will always collide with the default route. You better change its pattern to

url: "App/Index/{name}"

Or perhaps to the more friendlier

url: "App/{name}"

Both with

defaults: new { controller = "App", action = "Index" }

So that your routes would look like:

routes.MapRoute(
    name: "app",
    url: "App/{name}",
    defaults: new { controller = "App", action = "Index" }
);

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

See MSDN

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

5 Comments

@MonaCoder, Does your routes look exactly like what in the answer? What's the Url you're trying?
I am getting Server Error in '/' Application. error I modified the defaults but still not able to land to page without the index
@MonaCoder, What does the error says? And what's the Url?
I see I fixed it but isn't this a little bit very specific? I mean suppose I have another controller called MapControoler, then I have to write same MapRoute() for that too? Is there any way to make it more generic?
@MonaCoder, You already have that "generic" capability through the default route that will let you reach any action using the {controller}/{action} pattern. Once you need to reach a specific action using a different parameter name in the last segment ({name} instead of {id}), the framework must be able to tell them apart, and that would mean a distinctive Url pattern to the more specific one. However, it's worth looking whether there's a solution that is based on the parameter index rather than the name (you can always use the query-string though: /App/Index?name=...).

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.