2011-10-13 439 views
1
使用Server.Mappath时

下面是我的代码片段:权限被拒绝的错误使用

string ApplPath = Server.MapPath("./"); 
    switch (ddlReportType.Text) 
    { 
     case "District-wise": 
      if (ddlDistrict.Text == "All") 
       Response.Redirect(ApplPath + "contentReportAllDistricts.aspx"); 
      else if (ddlDistrict.Text != "All" && ddlDistrict.Text != "-- Select --") 
       Response.Redirect(ApplPath + "contentReportSelectedBlocks.aspx?" + ddlDistrict.Text); 
      break; 
    } 

当我不使用Server.MapPath,该应用程序运行良好,但现在,IE调试器显示错误:Permisson Denied

我正在使用本地主机并从本地主机本身运行应用程序。

回答

1

将使用Server.Mappath的物理路径返回到您的网站(例如C:/ mysite的/ ...),你应该改变你的代码如下所示,从根获得的路径:

switch (ddlReportType.Text) 
    { 
     case "District-wise": 
      if (ddlDistrict.Text == "All") 
       Response.Redirect("~/contentReportAllDistricts.aspx"); 
      else if (ddlDistrict.Text != "All" && ddlDistrict.Text != "-- Select --") 
       Response.Redirect("~/contentReportSelectedBlocks.aspx?" + ddlDistrict.Text); 
      break; 
    } 

〜符号将解析为您网站的根。

+0

我在我的根应用程序中有一个文件夹。路径。该页面位于该文件夹中。当我使用你的建议时,它显示错误:找不到资源。很明显,我应该在路径中追加文件夹名称,例如:“〜/ myFolder/contentReportAllDistricts.aspx”,但是有什么办法可以获取正在运行的页面的整个路径吗? – RKh

+1

@RPK - 您可以使用Request.PhysicalPath – adatapost

+0

@AVD它的工作原理,但它也挑选正在运行的页面名称。我只想要直到当前文件夹名称的路径。有什么方法可以丢弃它正在选择的页面名称吗? – RKh