test123123
test123123

Reputation: 921

View Cannot be Found.

My Error is as follows:

Server Error in '/' Application.

The view 'Login' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/User/Login.aspx
~/Views/User/Login.ascx
~/Views/Shared/Login.aspx
~/Views/Shared/Login.ascx
~/Views/User/Login.cshtml
~/Views/User/Login.vbhtml
~/Views/Shared/Login.cshtml
~/Views/Shared/Login.vbhtml^

Views/Login/Login.cshtml:

@{
    ViewBag.Title = "Login";
}
@{
    if (!ViewData["uname"].Equals(null) )
    {
            <div>
            Username: @ViewData["uname"]
            @Session["uname"] = @ViewData["uname"]
            </div>
    }
}
<h2>Login</h2>
<h4>Username:</h4>
<input type="text" name="uname" value=" " />
<button />

Controllers/Usercontroller.cs:

 public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }

    }

Global.asax.cs(only the method):

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                //new { controller = "Product", action = "List", id = UrlParameter.Optional } // Parameter defaults
                   new { controller = "User", action = "Login", id = UrlParameter.Optional }

                );

        }

Upvotes: 1

Views: 397

Answers (2)

Sergey
Sergey

Reputation: 8071

1) "User" should be the folder under Views. "Login" should be cshtml file or "Login" should be aspx file.

2) Provide full name in

public ActionResult Login()
{
    return View("~/User/Login");
}

Upvotes: 1

Kristoffer Lindvall
Kristoffer Lindvall

Reputation: 2682

You say your view is in Views/Login folder, but it should be in the Views/User folder.

It's found in the following way Views/<ControllerName>/<ActionName>.cshtml.

Upvotes: 5

Related Questions