7

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.

4
  • This code should work fine. Are you sure the view file is included in the project/solution ? Commented Jul 30, 2016 at 16:07
  • You shouldnt need the entire path of the view in return view. Also, why do you have an empty route decorator on top? Commented Jul 30, 2016 at 16:08
  • while the entire path is not needed, It will not break anything.It should still work. The empty route is to make this action the default. Commented Jul 30, 2016 at 16:10
  • Yes, the view file is included in the project. As I mentioned, when I do not use the query string it works just fine. The problem occurs when the query string is not empty. Commented Jul 30, 2016 at 16:16

1 Answer 1

8

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

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

Comments

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.