为什么使用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);
}
}
这是stacktrace:
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
----------------------------------
我该如何解决?
答案 0 :(得分:3)
您的代码似乎有点儿错误
你有一个Path.Combine
但是只有一个文件夹路径作为参数 - 它可以工作但是组合用于组合路径部分。您确定没有丢失路径部分,例如更多文件夹或文件名吗?
接下来,你Server.MapPath
到一个文件夹,但用File.Exists
检查它是否存在 - 这不会起作用,要么使它成为文件的路径,要么用{{1}检查它}}
Server.MapPath并不关心不存在的文件夹和文件。它只是告诉你传递它的相对文件的绝对路径是什么。它没有检查任何有关它的事情。如果您计划将文件写入此路径,则可以先安全地在其上调用Directory.Exists
以确保该目录存在,然后再将该文件写入其中。如果目录已经存在,则此调用是非操作。例如:
Directory.CreateDirectory