简体   繁体   中英

How To Change Default View In MVC 4 Web Application

By default, MVC 4 in Visual Studio 2017 sets _Layout.cshtml as the default layout for all pages. I believe it's doing this in App_Start/RouteConfig.cs :

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

(Index is set as the home page)

在此输入图像描述

I'm still not sure how Index is getting _Layout.cshtml . But what if I'm trying to set a different view - a login page - as the home page, like this?

在此输入图像描述

Also I'm trying to get rid of the Reports, Accounts, Settings, and Logout <li>'s in the header so that the page matches the design above. I'll also need a container with a form inside of it.

I've tried creating a _Login view inside of /Home and /Shared and changed "Index" to "Login" in App_Start/RouteConfig.cs :

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }

在此输入图像描述

But that's giving me an error:

在此输入图像描述

How can I create a view and set that view as the default view for this MVC 4 Web App? Thanks

What you see in the defaults parameters as action is the name of the controller's method, not view, so you should create a method named Login in the Home controller and create the associated View for that (In the Login method right click and choose Add View). Then it will act as the default home page.

defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }

So your Home controller looks like this:

public class HomeController : Controller
{
    public IActionResult Login()
    {
        return View();
    }
    //other codes   
 }

Also if you do not want the default layout to be used in the Login page you may add this at top of the Login page

@{
    Layout = "";
}

The error you see doesn't seem to be because of the layout page.

This error is because the Login Action is missing in the Home controller.

You see, The defaults specified are Controlller="Home", Action="Login" .
ie The compiler looks for the Login action in the Home controller. and when it doesn't find, it throws this error!

you could get rid of it by adding the login action like:

public ActionResult Login(string Uname, string Password)
{
    return View();
}

in the home controller! That's for the error in the question.

Here is the solution for your problem. You could add a different layout for each of your views by adding a razor code like below, to specify the layout for the view.

@{
   Layout = "~/Views/Shared/_Layout.cshtml";
                  //This is the path to the layout. You could change this to your custom layout's path.
}

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM