0

Below is the Controller code:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public string Index(string name)
    {
        return "Welcome to MVC_Demo"+name;
    }

}

and below is the Global.asax.cs codes:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }
}

When I run the application and browse for (http://localhost/MVC_Demo/home/index/pradeep) it shows only, "Welcome to MVC_Demo" as the output, and not as "Welcome to MVC_Demo Pradeep" i.e the parameter name "Pradeep" is not getting displayed.

Considering me just a beginner any help would be highly appreciated.


1
  • 3
    Because you method need to be public string Index(string id) so that you match your route (which expect a parameter named id). Or you need to create a specific route. Commented Apr 17, 2018 at 5:58

1 Answer 1

2

As your route states, the default parametername is id. So either change the default parametername to be name like this

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

Or you can change the name of the parameter to be id and match the route.

public string Index(string id)
{
    return $"Welcome to MVC_Demo {id}";
}

One more solution is to specify the parametername explicitly as a QueryString parameter:

http://localhost/MVC_Demo/home/index?name=pradeep

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

1 Comment

No problem @pradeep.

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.