I have set up custom error pages on my site using
<customErrors mode="RemoteOnly" defaultRedirect="~/Error">
<error statusCode="500" redirect="~/Error/InternalError"/>
<error statusCode="404" redirect="~/Error/FileNotFound"/>
<error statusCode="403" redirect="~/Error/AccessDenied"/>
</customErrors>
however there is another area on the site, Suppliers, and when an error occurs in the supplier area the redirect goes to Suppliers/Error/_. Since I don't have any error pages here, the site just seems to hang never shows the error pages. How can I fix this without having to copy the error pages to the supplier area?
As far as I understand with MVC your URL make up, by default is:
Domain/Controller/Action/id
If you have an "Error" Controller. In your logic, you test to see if the request originated from an user of the site that would need to redirect to the "Suppliers" Error page
[HandleError]
public ActionResult Index()
{
// Test to see if you need to go to the SuppliersController
if (this.User.IsInRole("supplier"))
{
return Redirect("/Suppliers/Error");
}
else
{
return View(); // This returns the "Error" View from the shared folder
}
}
redirect to an Error handling action on your Suppliers Controller that will return the right view.
public class SuppliersController : Controller
{
//
// GET: /Suppliers/
public ActionResult Error()
{
return View("Error","SomeMasterPage"); // No "Error" view in Suppliers View folder, so it uses the one in shared folder
}
}
You can also use the [Authorize]
attribute on your Suppliers Error Action to make sure the user is logged on.
In this way, you will get your desired /Suppliers/Error URL and can use the SuppliersController Action to specify the desired view, model, and master/Layout page.
also look at this very comprehensive reply to a similar question:
Best 404 example I can find for mvc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With