0

I created a custom AuthorizeAttribute which sends users to an error page if they don't have access to view the controller being requested.

The code that redirects them is as follows:

Protected Overrides Sub HandleUnauthorizedRequest(filterContext As AuthorizationContext)
    Dim result = New ViewResult()
    result.ViewName = "Error"
    result.ViewBag.ErrorMessage = "oops, you are not allowed"
    filterContext.Result = result
End Sub

It redirects them to ~/Shared/Error.vbhtml, just like it should; however, the page only loads my layout and includes the header "Error" but doesn't not load any content of the error page.

The contents of the error page are as follows:

@ModelType System.Web.Mvc.HandleErrorInfo

@Code
    ViewData("Title") = "Error"
End Code

@ViewBag.ErrorMessage

<h2>
    Sorry, an error occurred while processing your request.
</h2>

Why isn't it loading the "Sorry, an error occurred..." message and how can I make it do so? Thank you.

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
user1477388
  • 20,790
  • 32
  • 144
  • 264
  • you might want to have a look at [this](http://stackoverflow.com/questions/5070339/custom-authorize-attribute) and [this](http://stackoverflow.com/questions/4664302/mvc-roleprovider-and-authorize-attribute) links – Yasser Shaikh Aug 07 '12 at 17:41
  • Are you sure that the `~/Views/Shared/Error.vbhtml` view is being rendered at all? Could there be some other view that is used? Like `Error.cshtml`? – Darin Dimitrov Aug 07 '12 at 17:43
  • @Darin: Yes, because when I change result.ViewName = "Error" to result.ViewName = "Error1," it gives me an error. – user1477388 Aug 07 '12 at 17:44
  • @Yasser: Can you show me how these apply? I have an idea but I would like to see an example if possible. – user1477388 Aug 07 '12 at 17:44
  • This doesn't mean that the correct view is being rendered. As I said you could have some other Error.cshtml or Error.vbhtml view somewhere else which is being used. – Darin Dimitrov Aug 07 '12 at 17:45
  • @Darin: I don't know how else to verify. – user1477388 Aug 07 '12 at 17:45
  • Delete the `Error.vbhtml` view and see if you get an error. – Darin Dimitrov Aug 07 '12 at 17:45
  • @DarinDimitrov: It appears as though I don't get the error, so maybe it isn't loading the view at all. How can I fix it? – user1477388 Aug 07 '12 at 17:47
  • No, it is not loading the view inside `~/Views/Shared`. You probably have some other view inside other location of your site. Search for it. – Darin Dimitrov Aug 07 '12 at 17:50
  • 1
    your current controller's corresponding View folder might be having a Error.vbhtml view, check for that and remove if present – Yasser Shaikh Aug 07 '12 at 17:51

1 Answers1

1

It redirects them to ~/Shared/Error.vbhtml, just like it should

No, I suspect this isn't the case. You probably have some other Error.cshtml or Error.vbhtml view that is being rendered. Search your site for the presence of such file somewhere else. Like for example ~/Views/XXX/Error.vbhtml where XXX is the controller that is currently being executed.

Or you could also explicitly specify which view to be loaded instead of relying on the established probing locations:

result.ViewName = "~/Views/Shared/Error.vbhtml"
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928