在ASP.NET MVC(C#)上提供静态文件 [英] Serving static file on ASP.NET MVC (C#)

查看:114
本文介绍了在ASP.NET MVC(C#)上提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对.NET框架完全陌生,我想知道什么是提供静态内容和访问用户生成的内容的最佳方法.

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.

假设我有以下文件:

logo.jpg
document.pdf
etc.doc

我应该在哪里(放在哪个文件夹上)?以及我如何访问它?

Where should i put this (on which folders) ? And how do i access it?

最后,将其带入另一个层次.假设我有一个允许用户上传文档的Web应用程序(比如说一些图片).我想知道的是文件夹/目录的结构应该如何?

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?

django/CI中的normaly我将具有一个文件夹,如下所示:

normaly in django / CI i would have a folder as follows:

应用程序/{所有与网络应用程序相关的文件夹和文件都在这里} 上传/{所有相关的用户生成的内容都按文件类型归类} 资产/{css-javascript-document-would-go-here}

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}

注意:我还没有做任何事情,也没有做任何尝试.仅仅是因为我不知道如何访问尝试静态内容.我曾经尝试访问位于Content/Scripts文件夹中的* .css或* .js文件.

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.

即:localhost:12345/Scripts/jquery.js

i.e: localhost:12345/Scripts/jquery.js

事实证明,这不是ASP.NET MVC中的简单过程

It turns out, it is not a straightforward process in ASP.NET MVC

哦, ,在某些情况下,我还想从css文件访问我的图像.这又使我感到困惑.

Oh by the way, in certain occasion, i would also like to access my images from my css file instead. Which raises yet another confusion to me.

我阅读了以下文章

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

但是它不适用于我当前的MVC版本(我正在使用MVC 5)

however it is not applicable to my current version of MVC (i am using MVC 5)

EDIT 2 :所以,这是我到目前为止一直在尝试的东西

EDIT 2: So, this is what i have been experimenting so far

  1. 我在根目录(与Models,Controllers等级别)上创建了一个名为 Assets 的文件夹.
  2. 我在内部资产中创建子文件夹(即图像,文档等)
  3. 然后我想直接从我的视图访问那些文件,我会像这样:

  1. I created a folder called Assets on the root directory (same level as Models, Controllers, etc).
  2. Inside Asset i create sub-folders (i.e. 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).

任何提示将不胜感激.

推荐答案

假设我有以下文件:logo.jpg,document.pdf等.doc,我应该放在哪里(在哪个文件夹上)?以及我如何访问它?

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?

您在ED​​IT 2上所做的一切都很好. MVC框架足够聪明,可以禁用字典浏览.如果您尝试资产/assets//assets/images,则将获得403.

What you had on EDIT 2 is completely fine. The MVC framework is smart enough to disable dictionary browsing. If you're trying to asset /assets/ or /assets/images, you will get a 403.

此外,即使您有一个名为"assets"的控制器,一个名为"images"的方法以及一个字符串id为"picture.png"的请求,该请求也不会触发该控制器方法.相反,它将仍然为您提供图像文件.看起来MVC优先于它找到的静态文件,而不是MVC路由?

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. 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?

假设我有一个网络应用程序,允许用户上传文档(比如说一些图片).我想知道的是文件夹/目录的结构应该如何?

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?

如果您打算仅将用户的上传内容作为物理文件存储在应用程序中(而不是将其保存到持久性存储中),则可以创建一个文件夹,例如"uploads"放在根上,然后将其上载到那里.

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.

我将通过其ID(希望它是GUID而不是整数ID)将其上传内容分离到上传"目录下的不同子文件夹中.万一您要在前端显示上传路径,则用户无法轻易猜测其他用户的上传位置:

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:

// 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.

在某些情况下,我还想从css文件访问图像.

In certain occasion, I would also like to access my images from my css file instead.

在这种情况下,您将必须使用相对URL,并且MVC的根默认为~/Content.例如,假设您要使用保存在/assets/images/下的picture.png作为块的背景图片:

In this case, you will have to use relative URLs, and MVC defaults its root to ~/Content. For example, let's say you want to use picture.png you saved under /assets/images/ as the background picture of a block:

#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 ~/:

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

对我来说,这不是一个可以接受的解决方案,如果将应用程序发布到服务器上的虚拟文件夹(而不是wwwroot)上,那将是非常麻烦的事情.

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.

对于设置这样的背景图像,我宁愿在@Url.Content()的帮助下使用内联样式,因为我不必担心它们的路径如何相互关联:

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:

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

这篇关于在ASP.NET MVC(C#)上提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆