[英]Server.MapPath Could not find a part of the path in ASP.net MVC
[英]c# Could not find a part of the path with Server.MapPath()
为什么使用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
----------------------------------
我该如何解决?
您的代码似乎有问题
您在其中有一个Path.Combine
,但是只有一个文件夹路径作为参数-可以使用,但Path.Combine
是用于组合路径部分的。 您确定没有路径部分丢失,例如更多文件夹或文件名吗?
接下来,将Server.MapPath
移至文件夹,但使用File.Exists
检查其是否存在-这将不起作用,将其设置为文件的路径,或者使用Directory.Exists
检查
Server.MapPath不在乎不存在的文件夹和文件。 它只是告诉您传递的相对文件的绝对路径是什么。 它不会对此进行任何检查。 如果计划将文件写入此路径,则可以先安全地在其上调用Directory.CreateDirectory
,以确保目录存在,然后再将文件写入其中。 如果该目录已经存在,则此调用为非操作。 例如:
var fullPath= Server.MapPath("/non/existing/folders/");
Directory.CreateDirectory(fullPath);
fullPath=Path.Combine(fullPath,"newfile.txt");
File.WriteAllText(fullPath, content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.