地图在asp.net mvc的物理文件路径 [英] Map the physical file path in asp.net mvc

查看:211
本文介绍了地图在asp.net mvc的物理文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从磁盘中读取XSLT文件在我的ASP.Net MVC控制器。什么我做的是以下内容:

 字符串的文件路径= HttpContext.Request.PhysicalApplicationPath;
文件路径+ =/Content/Xsl/pubmed.xslt;
字符串XSL = System.IO.File.ReadAllText(文件路径);

不过,一半时这对forums.asp.net 线程有以下报价


  

HttpContext.Current是邪恶的,如果你
  在你的MVC应用程序的任何地方使用它,你
  正在做的事情错了,因为你
  不需要它。


虽然我没有使用电流,我想知道什么是决定一个MVC文件的绝对物理路径的最佳方式?出于某种原因(我不知道为什么!)的HttpContext 感觉并不适合我。

有没有更好的(或推荐/最佳实践)的ASP.Net MVC?

从磁盘读取文件的方式
解决方案

 字符串文件路径=使用Server.Mappath(Url.Content(〜/内容/ XSL /));

我不同意的想法, HttpContext.Current 是恶。这不是为每一个问题的锤子,但它肯定是比好,例如会话的东西,它可以做确定。

I am trying to read an XSLT file from disk in my ASP.Net MVC controller. What I am doing is the following:

string filepath = HttpContext.Request.PhysicalApplicationPath;
filepath += "/Content/Xsl/pubmed.xslt";
string xsl = System.IO.File.ReadAllText(filepath);

However, half way down this thread on forums.asp.net there is the following quote

HttpContext.Current is evil and if you use it anywhere in your mvc app you are doing something wrong because you do not need it.

Whilst I am not using Current, I am wondering what is the best way to determine the absolute physical path of a file in MVC? For some reason (I don't know why!) HttpContext doesn't feel right for me.

Is there a better (or recommended/best practice) way of reading files from disk in ASP.Net MVC?

解决方案

string filePath = Server.MapPath(Url.Content("~/Content/Xsl/"));

I disagree with the idea that HttpContext.Current is "evil." It's not the hammer for every problem, but it is certainly better than, e.g., Session for stuff that it can do OK.

这篇关于地图在asp.net mvc的物理文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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