To handle that scenario, you need to insert a custom controller factory into your ASP.NET MVC processing pipeline (the controller factory is the class responsible for instantiating the controller specified by your route). The final and most complicated URL-related error to handle is when the URL provided matches one of your routes, but the controller name provided isn't correct. Protected Overrides Sub HandleUnknownAction(actionName As String) This example of a HandleUnknownAction method returns a View called ErrorHandler to the user: The ExecuteResult method must be passed an MVC ControllerContext object, but, fortunately, you have access to one of those through the ControllerContext property on your Controller class. However, instead of returning the View to ASP.NET MVC so that it can process your View, you process the View yourself by calling the View's ExecuteResult method. There is a workaround, though, and you still get to use the controller's View method to generate a View. The only problem is that the HandleUnknownAction method doesn't return a value so you can't just return the results of the View method, passing the name of your more helpful review. Handling the case where the URL provided does match one of your routes and the controller name is right but the action name isn't just requires you to override the controller's HandleUnknownAction method. If you're following my advice about building routes and not including action methods and controller names in your URLs, this is the only case you need to handle - you can ignore the rest of this column because your users can never provide a bad controller or action method name.īut if you're embedding action method names in your URLs, then the good news is that the other two cases aren't much more difficult to handle. I have nothing against the default ASP.NET MVC route of _ This column shows how to provide a custom View in each of those cases but, first, a discussion on avoiding the problem altogether. The action method doesn't exist in the controller.The user's URL doesn't match any of your routing patterns.There are, essentially, three URL-related errors you need to handle: If nothing else, you can use these strategies to signal to yourself when you've provided an incorrect URL. I do know that I get my URLs wrong during development all. I don't know how often these kinds of errors occur in production sites. Alternatively, if you think users will be rewriting URLs in their browser's address bar (a practice called URL butchery), you might even want to take a guess at what the user really wanted and send them to the correct page by handling common misspellings. You might prefer to provide a View with a friendlier message or even View with links leading to actual pages on your site. If those URLs don't include a controller or action method name, then you can provide the missing information through default values on your routes.īut if those URLs include an incorrect action or controller name, then ASP.NET MVC will simply return one of the standard HTTP error codes. ASP.NET MVC is driven by the URLs your users provide to get to the Views they want to see.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |