Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET View Not Found

I created a Web API project in Visual Studio. I am using attribute routing. Here is the controller under Controllers folder:

public class RegistrationController : Controller
{
    // GET: Registration
    [Route("")]
    public ActionResult CreateUser(string platform)
    {

         return View("~/Views/Registration/CreateUser.cshtml", platform);
    }
}

When I call the CreateUser action by the URL http://localhost/application it works but when I try to pass a query string parameter by the URL http://localhost/application?platform=android, it gives the following error:

The view '~/Views/Registration/CreateUser.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Registration/CreateUser.cshtml

~/Views/Registration/android.master

~/Views/Shared/android.master

~/Views/Registration/android.cshtml

~/Views/Registration/android.vbhtml

~/Views/Shared/android.cshtml

~/Views/Shared/android.vbhtml

I cannot understand why it cannot find the view when it is there or why it even tries to find a view with the name of the query string parameter.

like image 671
John L. Avatar asked Jul 30 '16 16:07

John L.


1 Answers

It can probably find the view. It is the Master page that it can't find.

That is because you are using the

class Controller : ... {
    ViewResult View(string viewName, string masterName);
}

overload method, which

Creates a System.Web.Mvc.ViewResult object using the view name and master-page name that renders a view to the response.

the clue was the fact that it included the parameter value in the search for a view. Because you passed platform parameter was a string, it matched the method called to the one that used the string viewName and string masterName parameters.

Controller has many over loads for the ViewResult View() method. In this case you probably wanted to pass the platform as an object model. You can fix this by using named arguments which would avoid the confusion by letting the compiler know which overload method you intended to call....

public class RegistrationController : Controller {
    // GET: Registration
    [Route("")]
    public ActionResult CreateUser(string platform) {
         return View("~/Views/Registration/CreateUser.cshtml", model: platform);
    }
}

From there everything should work as expected

like image 143
Nkosi Avatar answered Nov 12 '22 15:11

Nkosi