RKh
RKh

Reputation: 14161

Permission Denied error when using Server.MapPath

Below is my code snippet:

   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;
     }

When I was not using Server.MapPath, the application was running well, but now, IE debugger displays error: Permisson Denied.

I am working on a local host and running the application from local host itself.

Upvotes: 1

Views: 1320

Answers (1)

Paddy
Paddy

Reputation: 33867

Server.MapPath will return the physical path to your site (e.g. C:/MySite/...), you should change your code as follows, to get the path from the root:

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; 
     } 

The ~ symbol will resolve to the root of your site.

Upvotes: 1

Related Questions