简体   繁体   English

在 ASP.NET MVC (C#) 上提供静态文件

[英]Serving static file on ASP.NET MVC (C#)

I am completely new to .NET framework and i would like to know what is the best way to serve static content and to access user generated content.我是 .NET 框架的新手,我想知道提供静态内容和访问用户生成内容的最佳方式是什么。

Suppose i have the following files:假设我有以下文件:

logo.jpg
document.pdf
etc.doc

Where should i put this (on which folders) ?我应该把它放在哪里(在哪个文件夹上)? And how do i access it?我如何访问它?

Lastly, taking it into another level.最后,把它带到另一个层次。 Suppose i have a web apps that allows user to upload a document (let say some pics).假设我有一个允许用户上传文档的网络应用程序(比如一些图片)。 What i would like to know is how should the structure of my folder/directory look like?我想知道的是我的文件夹/目录的结构应该是什么样的?

normaly in django / CI i would have a folder as follows:通常在 django/CI 中,我会有一个如下文件夹:

application/{all-web-app-related-folder-and-file-goes-here} uploads/{all-related-user-generated-content-goes-here-categorized-by-file-type} assets/{css-javascript-document-would-go-here}应用程序/{all-web-app-related-folder-and-file-goes-here} 上传/{all-related-user-generated-content-goes-here-categorized-by-file-type} assets/{css -javascript-document-would-go-here}

note: i haven't done anything or made any attempt to it.注意:我没有做任何事情或尝试做任何事情。 Simply because i have no idea how to access try static content.仅仅因为我不知道如何访问尝试静态内容。 I once tried to access the *.css or *.js files located inside Content/Scripts folder.我曾经尝试访问位于 Content/Scripts 文件夹中的 *.css 或 *.js 文件。

ie: localhost:12345/Scripts/jquery.js即:本地主机:12345/Scripts/jquery.js

It turns out, it is not a straightforward process in ASP.NET MVC事实证明,这在 ASP.NET MVC 中并不是一个简单的过程

Oh by the way, in certain occasion, i would also like to access my images from my css file instead.哦,顺便说一句,在某些情况下,我还想从我的 css 文件中访问我的图像。 Which raises yet another confusion to me.这给我带来了另一个困惑。

EDIT 1: I read the following article编辑 1:我阅读了以下文章

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/working-with-images-in-Asp-Net-mvc-framework/ http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/working-with-images-in-Asp-Net-mvc-framework/

however it is not applicable to my current version of MVC (i am using MVC 5)但是它不适用于我当前的 MVC 版本(我使用的是 MVC 5)

EDIT 2 : So, this is what i have been experimenting so far编辑 2 :所以,这是我迄今为止一直在试验的

  1. I created a folder called Assets on the root directory (same level as Models, Controllers, etc).我在根目录(与模型、控制器等相同级别)创建了一个名为Assets的文件夹。
  2. Inside Asset i create sub-folders (ie images, documents, etc)在资产内部我创建子文件夹(即图像、文档等)
  3. Then i want to access those file directly from my views i would go like this:然后我想直接从我的视图中访问这些文件,我会这样:

    img src="@Url.Content("~/Asset/images/picture.png")" img src="@Url.Content("~/Asset/images/picture.png")"

The above example is appropriate if i want to access image file.如果我想访问图像文件,上面的例子是合适的。

I however not sure if this is the ideal way to do it.但是,我不确定这是否是理想的方法。 It is however, the simplest solution i can come across at the moment (or should i say, rather naive).然而,这是我目前能遇到的最简单的解决方案(或者我应该说,相当天真)。

Any hints would be appreciated.任何提示将不胜感激。

Suppose I have the following files: logo.jpg, document.pdf, etc.doc, where should I put this (on which folders) ?假设我有以下文件:logo.jpg、document.pdf 等.doc,我应该把它放在哪里(在哪个文件夹上)? And how do i access it?我如何访问它?

What you had on EDIT 2 is completely fine.您在 EDIT 2 上的内容完全没问题。 The MVC framework is smart enough to disable dictionary browsing. MVC 框架足够智能,可以禁用字典浏览。 If you're trying to asset /assets/ or /assets/images , you will get a 403.如果您尝试资产/assets//assets/images ,您将获得 403。

Moreover, even you have a controller named "assets", a method called "images", and a string id "picture.png", the request won't hit the controller method.此外,即使您有一个名为“assets”的控制器、一个名为“images”的方法和一个字符串 id“picture.png”,请求也不会命中控制器方法。 Instead, it will still serve you the image file.相反,它仍然会为您提供图像文件。 It looks like MVC takes the precedence of the static files it finds over the MVC routings?看起来 MVC 优先于它找到的静态文件而不是 MVC 路由?


Suppose I have a web apps that allows user to upload a document (let's say some pics).假设我有一个允许用户上传文档的网络应用程序(比如一些图片)。 What I would like to know is how should the structure of my folder/directory look like?我想知道的是我的文件夹/目录的结构应该是什么样的?

If you're planning to just store user's uploads as physical files in the application (rather than saving them to a persistence store), you can create a folder like "uploads" on the root and put their uploads there.如果您打算仅将用户上传的内容作为物理文件存储在应用程序中(而不是将它们保存到持久性存储中),您可以在根目录上创建一个类似于“上传”的文件夹并将其上传内容放在那里。

I would separate their uploads by their IDs (hopefully it's a GUID rather than just integer IDs) into different sub folders under "uploads" just in case when you want to display the upload path on the front end, the user cannot easily guess where other users' uploads are:我会通过他们的 ID(希望它是一个 GUID 而不仅仅是整数 ID)将他们的上传分开到“上传”下的不同子文件夹中,以防万一你想在前端显示上传路径,用户无法轻易猜到其他用户上传的是:

// assets
// uploads
//     xxxx-xxxx-xxxx-xxxx
//         files
//     xxxx-xxxx-xxxx-xxxx
//         files

Don't forget to set this uploads folder's permission on the server you publish the app to.不要忘记在您发布应用程序的服务器上设置此上传​​文件夹的权限。


In certain occasion, I would also like to access my images from my css file instead.在某些情况下,我还想从我的 css 文件中访问我的图像。

In this case, you will have to use relative URLs, and MVC defaults its root to ~/Content .在这种情况下,您将不得不使用相对 URL,并且 MVC 将其根目录默认为~/Content For example, let's say you want to use picture.png you saved under /assets/images/ as the background picture of a block:例如,假设您想使用保存在 /assets/images/ 下的图片.png 作为块的背景图片:

#test-block {
    width: 250px;
    height: 250px;
    background-image: url(`~/assets/images/picture.png`);'
    background-size: cover;
}

You think it would access the picture correctly and use it as the background of the block.您认为它会正确访问图片并将其用作块的背景。 But in fact, it would generate something like this:但实际上,它会生成如下内容:

background-image: localhost/content/~/assets/images/picture.png

This is a bummer because you will have to manually go back up 1 level to grab the image, using ../ instead of ~/ :这是一个无赖,因为您必须手动返回 1 级以获取图像,使用../而不是~/

background-image: url(`../assets/images/picture.png`);'

To me, this is not an accepted solution, and it would be so buggy if you publish the app to a virtual folder on the server, which is not the wwwroot.对我来说,这不是一个可以接受的解决方案,如果您将应用程序发布到服务器上的虚拟文件夹(而不是 wwwroot),那将会很麻烦。

For setting background images like this, I would prefer to use inline styles with the help of @Url.Content() because I don't need to worry about how their paths are related to each other:为了设置这样的背景图像,我更喜欢在@Url.Content()的帮助下使用内联样式,因为我不需要担心它们的路径是如何相互关联的:

<div id="test-block" 
    style="background-image: url('@Url.Content("~/assets/images/picture.png")');">
</div>

ASP.NET MVC allows static file serving by default. ASP.NET MVC 默认允许静态文件服务。 Instead of using @URL.Content directly write the path starting with / denoting the root folder而不是使用@URL.Content 直接写以 / 开头的路径,表示根文件夹

<img src="/Asset/images/picture.png" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM