BoundForGlory
BoundForGlory

Reputation: 4427

Change @RenderBody to point to different View MVC3

By default, the @RenderBody in _Layout.cshtml in an MVC3 app points to ~/Views/Home/Index.

  @RenderBody()

Where is this set and how do I change it to point to ~/Views/Account/Logon? Or wherever I want. Thanks

Upvotes: 6

Views: 5223

Answers (3)

Triet Doan
Triet Doan

Reputation: 12083

You should use @RenderPage instead. Follow this link for more information.

Upvotes: 0

CD Smith
CD Smith

Reputation: 6617

It doesn't point to that view, it merely renders the view that it is given

Your app starts up and goes to the default action on the routing which can be found in Global.asax

You can modify that to default to /Account/LogOn if you wish

public class MvcApplication : System.Web.HttpApplication {

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start() {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
}

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039528

RenderBody doesn't point by default to ~/Views/Home/Index. It renders the view that was returned by the controller action that was executed. And since in your Global.asax in the routing definition the default action is configured to be Index, it is this view that is rendered.

So all you have to do is modify your routing configuration so that the default action is Logon on the Account controller:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);

Now when you navigate to /, the LogOn action of the Account controller will be executed which itself will render the ~/Views/Account/LogOn.cshtml view.

Upvotes: 1

Related Questions