Although you can redefine the layout from the controller, as suggested in another answer, in my opinion, this means that the controllers are too involved in determining what the user interface will be. It is best to leave it pure in views to decide.
The closest thing to what you are asking is to do this in the current "~/Views/_ViewStart.cshtml"
:
@{ if(Context.Request.Path.StartsWith("/External", StringComparison.OrdinalIgnoreCase)) Layout = "~/Views/_ExternalLayout.cshtml"; else Layout = "~/Views/_Layout.cshtml"; }
Where "~/Views/_ExternalLayout.cshtml"
is your alternate layout.
It may be worth checking that the lead "/"
correct, I canβt remember if it is.
If you put this in an existing _ViewStart, then any view that is executed in response to a URL starting with "/External"
will use this new layout, otherwise normal will be used.
Another approach is to use a routing table to add a route value that can be used here to make a layout decision; but I went for this approach to make things simple.
source share