Include Header File in _Layout Page - asp.net-core

I am trying to include a header file in my _Layout.cshtml page in asp.net core MVC application. The header.cshtml is located under root folder under wwwroot\inetpub, not under ~\Views\Shared\ , ie. \include\header.cshtml. I have tried to use #Html.Partial("/include/header"), #Html.Partial("/include/header.cshtml"), but I get error for "InvalidOperationException: The partial view '/include/header' was not found. The following locations were searched: /include/header". Please advise.

Use relative paths, like this:
#Html.Partial("../../wwwroot/include/header")
Here is the directory structure:

Related

Can I specify a _ViewImports.cshtml file from elsewhere in the folder hierarchy?

I have a _ViewImports.cshtml file in my Views folder.
I have another view outside my views folder for which I'd like to use the same set of imports.
Is there a way to do this?
I was envisaging specifying the full path to \Views\_ViewImports.cshtml file at the top of my razor .cshtml file.

How do i access a file inside the angular 10

My folder structure of Angular 10 project is given below
-Backend
-e2e
-node_modules
-src
I want to access backend/uploads/profilepic.jpg in the angular's index.html page. Simply, i just want to load an image file in the front end which is uploaded in the backend folders.
A static resource, like an image, is either bundled inside the angular webapp, by adding it to the assets folder (e.g. 'src\assets\images') and have that folder referenced inside angular.json:
"assets": [
"src/assets"
],
Or, make the backend serve that image and simply access it from the frontend like this:
<img alt="xyz" src="https://yourdomain/img_test.png">
If both the backend and frontend are hosted on the same domain, you can use relative path for the URL, i.e https://yourdomain can be omitted.
Finally, I found the solution. In order to access the files of the 'uploads' folder which is located in the backend folder. I have to make that folder static. for that in my server.js file, I have added below code
app.use('*/uploads',express.static('uploads'));
after that, I could load my image file from that uploads folder by calling
http://localhost:8090/uploads/image.jpg

404 error serving .js files on ASP.NET Core

I've got a straightforward ASP.NET Core web app using Razor pages (not MVC). I've been looking at the Application Insights as I run the app and see a ton of 404 errors related to files like this...
https://localhost:5001/~/lib/bootstrap/dist/js/bootstrap.bundle.js
My Startup class has app.UseStaticFiles(); and is able to serve other files in the root directory and sub directories like css and images. So why can't it find and serve these .js files? I've tried adding options to the static file configuration but nothing seems to work. I keep getting the 404 errors in Application Insights. I can go to the file explorer and see that these files all exist. I've been working on this for hours with no success.
The ~ should be changed to the root of the application (which might be the root of the domain, or a virtual directory). If it's not, then it's not being used correctly.
My guess is that you have a slash in front:
<script src="/~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
The path should begin with a ~, not a slash:
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
I figured it out. My clue was that in the empty template project in the Layout.cshtml some of the tags like <environment> were displayed in bold but in my project they weren't. In my ViewImports file I included #tagHelperPrefix th: which makes it so the Microsoft tag helpers require special syntax like <th:link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />. The Microsoft tag helpers must do something special with paths that involve a ~ character.
When I replaced all my link tags in Layout.cshtml from <link> to <th:link> it started to work. Or I can take out #tagHelperPrefix th: from ViewImports and use the normal looking tags.

How to use Free CSS Templates with ASP.NET MVC 4?

I need to use a css template for webpage I am building but I don't know how to use in asp.net mvc4. so How to use CSS Templates with ASP.NET MVC 4 in visual studio 2012 for web application?
It's simple:
Move your css and images into ~/Contents folder.
Move your js files into ~/Scripts folder
Copy and paste your html snippets to cshtml files
On 3rd step you need to port the layout portion of your template to ~/Veiws/Shared/_Layout.cshtml. Here you need to point references to your css and js correctly. For example:
And also you need to be aware that _Layout.cshtml should contain #RenderBody(). That's where all child views will be inserted.
Just found a video with basic demonstration of process: link
It is the same as you use for simpe HTML files. In the solution you can add an existing item and your browse your CSS file which will be added in the Content folder then you have to reference it in the page where it will be used.
If you want to apply all the rules to every page you can reference it in the _Layout.cshtml page which is called before everypage. It is like the container.
You have many options to call it. I will suggest you also reading Bundling and minification in this link which is also interesting to know.
There are Few Steps:
Make Directory inside Content folder in my case mytemplate.
Copy all your template content inside mytemplate folder.
Add new Layout page in View>Shared Folder in my case _mytemplate.cshtml.
open your template Index.html file inside any editor(i.e: Dreamviewer,Notepad etc).
Copy all header stuff from your html file to inside of Header in _mytemplate.cshtml file.
Copy all body stuff Inside of your Index.cshtml to Layout page.
Save your Project by Cntrl+S and Run it in browser. Thats it.

Read Global resource file inside controller

How can I read global resource file in MVC controller, I am using this code and it is not working
Resources.BillingResource.Error;
returns the following exception
Could not load file or assembly 'Resources' or one of its dependencies. The system cannot find the file specified.
My application is MVC 4 and framework 4.5
Please advice
Is the resource file loaded in the same project or is on the different project. If its different project , you would have to add the reference to it.
Also check the namespace generated by the cs file of the resource file and reference that in your view or controller.