ASP MCV: index action with optional row parameter

I would like to have an index action with an optional string parameter. I can not make it work.

I need the following routes:

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

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

How to determine the route and controller?

This is the definition of my route:

 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 is this not working? I am not an expert in ASP MVC, but this seems to be a very simple problem.

Error

  • When I go to http://mysite/downloads , it works fine
  • When I go to http://mysite/downloads/4.5.6 , the controller is called correctly, and this parameter is passed correctly. But then it seems that the look was not found. This is the error I found:

enter image description here

+5
source share
7 answers

The error of passing the parameter to the view is fixed as follows:

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

or

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

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

 View(String viewName) 
+4
source

Your Download route contradicts your Default route. Comment on the Download route and it will probably work.

BTW you can install RouteDebugger to find out these problems for yourself.

+1
source

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). You are intentionally looking for a view with this name, in which case it should be in the Views / Download folder or in your project. If, however, you just want to pass it to the default view as the variable that will be used on the page, it is best to use it in the model, or you can simply insert it into the ViewBag if it is one.

Also you do not need to use:

 Public ActionResult Index(string version) 

Instead, you can use routingata, for example.

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

Hope this helps

+1
source

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

You can set a default value for your parameter:

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

You did not specify an action name in the URL, for example {action} you can try:

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

I am sure because in the View you indicate that this is an optional parameter, but your controller says that it is required. Change the signature of your index method to expect a null parameter

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

Why you do not have two methods in your boot controller:

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

Source: https://habr.com/ru/post/1202664/


All Articles