为什么使用Server.MapPath()
会导致以下错误:
找不到路径'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.的一部分
这是我的代码:
public ActionResult Index(int? page, string sort, string filter)
{
try
{
string path1 = Path.Combine(Server.MapPath("~/SomeFolder/myFiles/"));
if (!System.IO.File.Exists(path1))
{
string createText = "Hello and Welcome" + Environment.NewLine;
System.IO.File.WriteAllText(path1, createText);
}
}
catch (Exception ex)
{
throw new Exception("HomeController/save text to file: " + ex.Message, ex);
}
}
下面是堆栈跟踪:
ErrorMessage: HomeController/save text to file: Could not find a part of the path 'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.<br/>
InnerException: Could not find a part of the path 'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.
StackTrace: at myProject.Controllers.HomeController.Index()
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Date :11/19/2017 9:27:50 AM
----------------------------------
我怎么才能修好它?
发布于 2017-11-19 07:34:57
你的代码似乎有点错误
这里有一个Path.Combine
,但是只有一个文件夹路径作为参数-它可以工作,但是组合是用于组合路径部分。您确定没有缺少路径部分,比如更多的文件夹或文件名吗?
接下来,您可以Server.MapPath
到一个文件夹,但是用File.Exists
检查它的存在--这是行不通的,要么让它成为文件的路径,要么用Directory.Exists
检查它。
Server.MapPath不关心不存在的文件夹和文件。它只是告诉您传递的相对文件的绝对路径是什么。它没有检查任何关于它的东西。如果计划将文件写入此路径,则可以首先安全地调用该路径上的Directory.CreateDirectory
,以确保该目录在写入该文件之前存在。如果dir已经存在,则此调用是非操作的。例如:
var fullPath= Server.MapPath("/non/existing/folders/");
Directory.CreateDirectory(fullPath);
fullPath=Path.Combine(fullPath,"newfile.txt");
File.WriteAllText(fullPath, content);
https://stackoverflow.com/questions/47371036
复制相似问题