Yitzhak Weinberg
Yitzhak Weinberg

Reputation: 2592

Do "if" in Application WebConfig if it is posible

I want to do like this - but I cent rite razor by web config. Is there a way to write razor or do it in a different way to my goal will only manager see the errors

Apologize in advance for my English

@using Or50Core.Controllers;
@if ((BaseController)this.ViewContext.Controller).IsAdministrator())
{
 <system.web>
    <customErrors mode="Off"></customErrors>
   </system.web>  
}else{
 <system.web>
    <customErrors mode="On"></customErrors>
   </system.web> 
}

"if" do the work in views

Upvotes: 0

Views: 92

Answers (3)

Kahbazi
Kahbazi

Reputation: 15015

You have to write Application_Error method in your Global.ascx. In this method you can check if current user is in Admin role or not and based on that you can show the real error or just a simple error page.

protected void Application_Error()
{
   if (!User.IsInRole("Administrator"))
   {
    var exception = Server.GetLastError();
    var httpException = exception as HttpException;
    Response.Clear();
    Server.ClearError();
    var routeData = new RouteData();
    routeData.Values["controller"] = "Errors";
    routeData.Values["action"] = "General";
    routeData.Values["exception"] = exception;
    Response.StatusCode = 500;
    if (httpException != null)
    {
        Response.StatusCode = httpException.GetHttpCode();
        switch (Response.StatusCode)
        {
            case 403:
                routeData.Values["action"] = "Http403";
                break;
            case 404:
                routeData.Values["action"] = "Http404";
                break;
        }
    }

    IController errorsController = new ErrorsController();
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    errorsController.Execute(rc);
    }
}

Here you determine what users see based on each error

public class ErrorsController : Controller
{
    public ActionResult General(Exception exception)
    {
        return Content("General failure", "text/plain");
    }

    public ActionResult Http404()
    {
        return Content("Not found", "text/plain");
    }

    public ActionResult Http403()
    {
        return Content("Forbidden", "text/plain");
    }
}

BTW I find the answer in Here

Upvotes: 1

Ali Soltani
Ali Soltani

Reputation: 9946

You can use this code:

@{
        var configuration = WebConfigurationManager.OpenWebConfiguration("~");
        var section = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

        if (section != null)
        {
            @if ((BaseController)this.ViewContext.Controller).IsAdministrator())
            {
                section.Mode = CustomErrorsMode.Off;
            }
            else
            {
                section.Mode = CustomErrorsMode.On;
            }                
        }
        configuration.Save(); 
}

this code needs to add @using System.Web.Configuration; to view.

Edit:

For manage users you can use ASP.NET Identity and for manage error page you can use Custom Error Page.

Upvotes: 1

Glenn Packer
Glenn Packer

Reputation: 221

you would be far better off using logging. That way you catch all the errors (not just ones the administrator gets) in the log files/DB but the users only get a friendly error.

Upvotes: 1

Related Questions