0

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

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
Yitzhak Weinberg
  • 2,324
  • 1
  • 17
  • 21
  • Does this idea help : http://www.beansoftware.com/ASP.NET-Tutorials/Multiple-Config.aspx – PaulF Jul 12 '16 at 07:43

3 Answers3

1

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.

Glenn Packer
  • 212
  • 1
  • 8
  • Can you please elaborate more on the way that you would recommend to collect data errors on the site, or alternatively refer me to the Demo on the Internet – Yitzhak Weinberg Jul 12 '16 at 08:12
  • for the unexpected errors i would use something like ELMAH SCot has an article on it [link](http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx) expected errors i would use something like nlog [link](https://github.com/NLog/NLog/wiki/Tutorial) – Glenn Packer Jul 12 '16 at 08:48
1

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.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • Replace your code in question with this code to your view – Ali Soltani Jul 12 '16 at 08:35
  • No Because he probably can not change the settings at runtime Secondly it can be problematic to my question because I do not want all users will see errors during the Admin login only The Admin seem possible to you – Yitzhak Weinberg Jul 12 '16 at 09:23
  • I test it,It can change web config in runtime. Also in if statement, you can manage users to see error or not. – Ali Soltani Jul 12 '16 at 09:41
  • I really appreciate your effort, but that does not help me   Because I want to administrator see errors every site, and normal user will see the default error page I know how to manage users – Yitzhak Weinberg Jul 12 '16 at 11:32
  • before of actions (for show error views), you can check user is admin or normal user and show views according to current user. – Ali Soltani Jul 12 '16 at 11:50
1

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

Community
  • 1
  • 1
Kahbazi
  • 14,331
  • 3
  • 45
  • 76