Daniel Peñalba
Daniel Peñalba

Reputation: 31847

ASP MCV: Index action with optional string parameter

I would like to have an Index action with an optional string parameter. I'm unable to make it work.

I need the following routes:

http://mysite/download
http://mysite/download/4.1.54.28

The first route will send a null model to the Index view, and the second one will send an string with the version number.

How can I define the route and the controller?

This is my route definition:

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

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

And this is the controller:

    public ActionResult Index(string version)
    {
        return View(version);
    }

Why does this not work? I'm not an expert in ASP MVC but this seems to be a very simple problem.

The error

enter image description here

Upvotes: 7

Views: 20517

Answers (7)

Anton Maximov
Anton Maximov

Reputation: 251

string? will not work because string is not a value type.

You can set a default value to your parameter:

public ActionResult Index(string version="")
{
    return View(version);
}

Upvotes: 7

Daniel Peñalba
Daniel Peñalba

Reputation: 31847

The issue is fixed passing the parameter to the view in the following way:

public ActionResult Index(string version)
{
    return View((object)version);
}

Or

public ActionResult Index(string version)
{
    return View("Index", version);
}

When you pass a string model to the view, if the model is a string parameter, it is interpreted as the view name due to the following overload method

View(String viewName)

Upvotes: 5

HuwD
HuwD

Reputation: 1800

Your controller is looking for a view with the same name as the version attribute entered in the url (e.g. 4.1.54.28). Are you intentionally looking for a view with that name, in which case it should be in the Views/Download folder or your project. If however you simply want to pass it to the default view as a variable to be used on the page your best off sticking it in a model or you can just stick it in ViewBag if it's a one off.

Also you don't need to use:

Public ActionResult Index(string version)

You can use routedata instead e.g.

Public ActionResult Index()
{
    string version = RouteData.Values["Version"].ToString();

    ViewBag.version = version;

    return View();
}

Hope this of some help

Upvotes: 1

CSharper
CSharper

Reputation: 5580

I'm pretty sure it's because in the View you state it is an optional parameter, but your controller says that it is mandatory. Change the signature of your index method to expect a nullable param

    public ActionResult Index(string? version)
    {
        return View(version);
    }

Upvotes: 0

C Bauer
C Bauer

Reputation: 5103

Why not have two methods in your download controller:

public ActionResult Index()
{
        return View();
}
[HttpGet, ActionName("Index")]
public ActionResult IndexWithVersion(string version)
{
        return View(version);
}

Upvotes: -1

Yehia Elhawary
Yehia Elhawary

Reputation: 598

You are not set action name in url like {action} you can try:

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

Upvotes: 0

demoncodemonkey
demoncodemonkey

Reputation: 11957

Your Download route is conflicting with your Default route. Comment out the Download route and it will probably work.

BTW you can install RouteDebugger to figure out these kind of problems for yourself.

Upvotes: 1

Related Questions