Reputation: 31847
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.
http://mysite/downloads
it works finehttp://mysite/downloads/4.5.6
, the controller is correctly called, and the parameter is correctly passed. But then seems that the view is not found. This is the error I found:Upvotes: 7
Views: 20517
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
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
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
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
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
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
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