As the title says, I want to know where I should save HTML files and images. Right now, it's very messy. I have all my controllers under a controller folder, but all my images and HTML files are just laying around. I tried putting my HTML files and my images under separate folders, but then they can't access them. How do I organize my files?
Asked
Active
Viewed 1,152 times
0
-
I think this you can decide on your own preferences, I always create a Content/assets folder in my (UI) project. ~/Content/Images ~/Content/Templates ~/Content/Html Just be always consistent, and have a logic structure for others working on your project. – Peter Jun 17 '20 at 20:41
-
Create an assets folder and place the images into that folder. – Anthony Jun 17 '20 at 20:42
-
Depends also if you're using asp.net WebForms, or mvc, or asp.net core razor pages. Asp.net comes in many flavours. You mentioned a Controllers folder which suggests MVC, but would be useful to confirm – ADyson Jun 17 '20 at 22:14
-
1Why would an ASP.NET application have **any** static HTML files? – Dai Jun 18 '20 at 01:38
-
I'm a little new to this (obviously). I'm pretty sure I'm using MVC. And for Dai, is there any other kind? – Queso Pez Jun 18 '20 at 02:16
-
*I tried putting my HTML files and my images under separate folders, but then they can't access them* This is the right approach. Sounds like you need to troubleshoot. I would do a search for questions like [this one](https://stackoverflow.com/questions/32136299/in-my-mvc-5-project-i-cant-access-static-html-page-when-working-on-local-iis) or [this one](https://stackoverflow.com/questions/4156188/unauthenticated-users-cannot-access-static-files-in-asp-net-mvc-regardless-of-lo) depending on your problem. – John Wu Jun 18 '20 at 03:06
-
Thanks! Turns out that, silly me, I didn't add the ../ to the beginning. However, I can't seem to set the index file location. – Queso Pez Jun 18 '20 at 04:25
-
Something to add: my very inexperienced self thought i was using MVC, but I was using web api (if that helps) – Queso Pez Jun 18 '20 at 04:36
-
Web API is meant to be accessed by other programs not by humans, the controllers generally accept and return JSON or XML in machine-readable format, rather than HTML. Again it's unclear why you'd want static HTML in an API project. An MVC project can run as a hybrid by adding some API controllers to it, so it you want a mixture of UI and API that might be the way to to. But even then, you should generally be using views rather than static HTML files. – ADyson Jun 18 '20 at 06:48
-
My current project is a website, and I have controllers doing all the back-end stuff for me, so I have some HTML files for my website pages. Is this fine? – Queso Pez Jun 18 '20 at 17:28
-
Also, I don't think I'm using MVC. I don't have a "views" folder, and all my controllers are inherited from the ApiController class. is it possible to "add" MVC back in? – Queso Pez Jun 18 '20 at 17:31
-
You said yourself you're using a web API project. Web API isn't designed to host pages. If you want some pages it would make sense to convert it to an MVC or Razor Pages project which are designed for hosting pages. That was my point in my last comment. You can still add API controllers (as opposed to MVC controllers which work slightly differently) to those projects. – ADyson Jun 19 '20 at 06:13
2 Answers
0
Here is an excellent guideline by Microsoft on how to arrange your static assets. Static files in ASP.NET Core
Ideally you should have a public (or assets) directory,inside you should have directories for file types like css,html,js . Also you need to set up the middleware so that ASP.net can serve them correctly.

m5c
- 567
- 8
- 13
0
If the project is ASP.NET Core MVC, you can place the files under the wwwroot
of the project.
And access it like this,
public class TestController : Controller
{
public IActionResult Index()
{
return View();
}
private IWebHostEnvironment _hostEnvironment;
public SendEmailController(IWebHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}
[HttpPost]
public IActionResult Index(EmailModel model)
{
// get the file path
string fileName = Path.Combine(_hostEnvironment.WebRootPath, "files", "file.pdf");
// code omitted
return View();
}
}

大陸北方網友
- 3,696
- 3
- 12
- 37