MVC4 can't find shared layout page - c#

I have an MVC4 web application that i'm adjusting. Currently a controller loads Index.cshtml. In that file i have the following line:
#{
Layout = "~/Views/Shared/_Layout-DoubleColumn.cshtml";
}
So i changed that line to:
#{
Layout = "~/Views/Shared/_Layout-TripleColumn.cshtml";
}
I also copied to _Layout-DoubleColumn.cshtml file and renamed that to _Layout-TripleColumn.cshtml.
Now when i run the application i get the following error message:
The layout page "~/Views/Shared/_Layout-TripleColumn.cshtml" could not
be found at the following path:
"~/Views/Shared/_Layout-TripleColumn.cshtml"
I have no clue what the problem could be, because i'm sure that the file is in the Shared folder...
Anyone any idea what the problem could be?

Make sure the Build Action of the file is set to Content in the properties of the View.

Related

ASP.NET Core layout is not loaded in my area

My layout name is = _DefaultLayout.
Controller:
_ViewStart:
Index.cshtml:
The CSS is not loaded in this page, but on another page it is loaded.
Please help my to fix this problem
Thanks
Seem like typo problem. The suffix for layout code is predefined. Therefore, change _DefaultLayout.csshtml to _DefaultLayout.cshtml.
Then in the _ViewStart.cshtml you can write:
#{
Layout = "~/Views/Shared/_DefaultLayout.cshtml";
}
And check that Build Action property of the _DefaultLayout.cshtml file is set to Content.
For additional information see: Layout in ASP.NET Core

Display image razor pages

I am trying to display images on my web api that are located on the server side.
for now I have added them in my solution in visual studio (I added two to try, I specified one as embedded ressource and one as none as build action. they are directly in WebAPISolution/xxx.png not in a subfolder
In my cshtml page, I put this:
<div>
<p>Test image</p>
<img id="img1" alt="Image1.png" src="#Url.Content("~/Image1.png")">
</div>
and on my web page it doesn't shows up.
it seems to look for it at:
view-source:https://xx.xx.xx.xx:5001/Image1.png
and the error message is
"can't be displayed because it contains error"
, it just doesn't find it I guess because the same with ImageXXX.png which doesn't exist return the same error
I don't get why the image doesn't shows up.
here is how is organized my solution folder
1 ) In your Startup Class you should add app.UseStaticFiles(); within public void Configure() method
2) Put your image in the wwwroot folder
3) add this code to your razor view <img src="~/Image1.png" />

Layout page not found executing a sample ASP.NET MVC project

The layout page "~/Views/Shared/_Layout.cshtml" could not be found at
the following path: "~/Views/Shared/_Layout.cshtml".
I am getting the above error from the _ViewState.cshtml
It was coming while I execute it through my Home Controller.
I have got to see some link for resolving this program but it is not happening. Could you people help me out on this
If you have your code like this
#{
ViewBag.Title = "title";
Layout = "_Layout";
}
Then make sure that in your ~/Views/_ViewStart.cshtml file you have set the correct path
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
The error message tells you exactly what is the problem.
Check the Views/Shared Folder and make sure the _Layout.cshtml is actually there.
The build action of that file should be Content

How to specify global MVC layout without specifying layout path in razor view

I'm trying to get away from having to manually specify the layout path in every Razor view that I have / create.
So in a razor view, you would normally specify the view / layout properties such as:
#{
ViewBag.Title = "About Us";
Layout = "~/Views/Shared/_ContentLayout.cshtml";
}
I have a base controller that all my controllers are inheriting, in which I would love to be able to specify the layout at this level, or alternatively in app_start etc.
For any exceptions I would just override this in the view itself.
After an extensive search, I haven't found any evidence of anyone being able to do this yet.
My current, next-best workaround is to specify this in the ViewBag, to keep it dynamic, but I still need to put a declaration in the view:
#{
Layout = ViewBag.Layout;
}
Is it possible? Solutions?
Reference: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts
Since MVC3 there is a convention where...
You can add a file called _ViewStart.cshtml (or _ViewStart.vbhtml for VB)
underneath the \Views folder of your project:
The _ViewStart file can be used to define common view code that you
want to execute at the start of each View’s rendering. For example, we
could write code within our _ViewStart.cshtml file to programmatically
set the Layout property for each View to be the _ContentLayout.cshtml file
by default:
#{Layout = "~/Views/Shared/_ContentLayout.cshtml";}
Because this code executes at the start of each View, we no longer
need to explicitly set the Layout in any of our individual view files
(except if we wanted to override the default value above).
As mentioned by Nkosi, if you want to adapt the layout to a per controller basis without specifying the layout path in the view, you could do a condition block in the _ViewStart file.
But what I've found also works and is a tiny bit easier...
Place a _ViewStart file in each view folder (which relates to a controller), which you want to have a different layout.
The more specific _ViewStart in the View Area folder overrides the global _ViewStart file.

MVC 4 Intranet application, multiple layout

I am getting an error in developing a MVC4 Intranet application, trying to use multiple page layouts. As I see, the problem is not present when Internet application is used, here is quick demo.
Create new Internet application using Razor engine. Copy file _Layout.cshtml to Popup_Layout.cshtml (both in ~/Views/Shared), and strip header and footer sections in Popup_Layout. In ~/Controllers/HomeController.cs modify Index action with following code:
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
string layout = "_Layout";
if (DateTime.Now.Ticks % 2 == 1)
{
layout = "Popup_Layout";
}
return View("Index", layout);
}
That creates a simple game which randomly chooses to use one of two available page layouts. When compiled and tested, with refresh in browser we can see start page with or without header and footer sections.
Now if we repeat the exactly same scenario with only difference that project is Intranet application - build goes OK, but with refreshes in browser instead of getting a page without header and footer we get the server error, with the top of Stack trace says:
[InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Popup_Layout.master
~/Views/Shared/Popup_Layout.master
~/Views/Home/Popup_Layout.cshtml
~/Views/Home/Popup_Layout.vbhtml
~/Views/Shared/Popup_Layout.cshtml
~/Views/Shared/Popup_Layout.vbhtml]
System.Web.Mvc.ViewResult.FindView(ControllerContext context) +399386
As an alternative, layout can be determined in View itself with assignment ~/Views/Shared/Popup_Layout.cshtml to Layout property, but it also throws an error.
I didn't find the solution using web, so I hope someone can help me here. Thanks
Try deleting Popup_Layout.cshtml then instead of copy pasting the _Layout.cshtml create a new empty view page for Popup_Layout. After you have a new empty view page copy paste the content (if you wish). I don't know why but I tried it on my test project and it worked. I used a regular web app (not an intranet app) but I experienced the same missing view error you described.
I think the key is the way the view file was created. I copied the file and renamed it and seen the same error. When I properly created a new view and then copied in the content it all worked.
Dear noviKorisnik i tried to replicate your error as per you written here but i am not getting the error as you specified .
Your error first line is
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
I believe mistakenly you deleted the index.cshtml .
Please be sure you have index.cshtml in the right folder .if you still get the error please give more detail .

Categories