Can MVC Custom Error pages retain Route Values? - c#-4.0

The MVC project that I am currently working on uses Regions so that we can localise pages etc.
I have spotted a problem with our Error page. We have turned the custom error pages on in the web.config file. If we are on a page lets say : /IT/News/Index and we get an error, when it redirects it will go to /Error and there will be no routevalue attached to it.
Is there away to ensure that the langauge routevalue is retained by the Error page?
I have searched around and cannot find a solution at the moment and was wondering if anyone else could help or point me in the right direction?
Hope that this all makes sense. Any help is much appreciated.

If you're getting physically redirected to /Error then it's not because of the MVC HandleErrorAttribute. It's probably due to your Web.Config having system.web/customErrors defined for error handling. Using the HandleErrorAttribute causes it to inject a specific view instead of the view you would have normally returned but does not redirect you to a different action by default. The problem is when redirected because of customErrors, there is no inherant information available to tell you where they came from. But using HandleErrorAttribute DOES cause some info to be populated for you. Specifically it creates a HandleErrorInfo to use as a view model and passes that to the view you specify. For example, here's one that is reigstered in the /App_Start/FilterConfig.cs file.
public class FilterConfig
{
public static void RegisterFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute {View = "Error"});
}
}
When you redirect to an error View using the HandleErrorAttribute, certain information is populated for you. The HandleErrorInfo view model will contain the ControllerName of the original controller requested, and the ActionName of the original action. Also, the ViewData and the TempData from the original request will be copied into the ViewData and Temp data for the request to the Error view. With that information it should have what you need. Be aware that not all errors happen inside of an Action however, and exceptions that don't happen in an action will not be caught by the HandleErrorAttribute. So you'll still need to use something like customErrors (or system.webServer/httpErrors if you're doing it inside of IIS7+) to handle exceptions that occur elsewhere in your app.
Here's a link to the HandleErrorAttribute file on CodePlex in case you're wondering what it does. HandleErrorAttribute.cs

I'm not sure if this solution meets you requirements. You can override in your base controller OnException and then redirect to a specific page.
protected override void OnException(ExceptionContext filterContext)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString();
//get other stuff from routing
//here you can do redirect or other stuff
//if handled exception
//filterContext.ExceptionHandled = true;
base.OnException(filterContext);
}

It depends how you're getting to the error pages, really. If you're using an ActionFilter-based method to catch exceptions, then you can get route values from the context that gets passed into the OnException method. If you're using a redirect from a catch block, then you can push the relevant information into TempData or pass it directly as a parameter, depending on how you're doing that redirect.

You can add a custom HandleErrorAttribute or use a base controller to be inherited by all your controllers. Either way, you need to get the RouteData object, like this
var routeData = filterContext.RouteData;
with that object, you can get all the route values accordingly to your needs. Check the object definition in MSDN site for more detail
Say you have the following route
routes.MapRoute(
"Language", // Route name
"{language}/{controller}/{action}/{id}", // URL with parameters
new { language = "en", controller = "Sites", action = "Index", id = UrlParameter.Optional } // Parameter default
Then routeData.Values.Keys will tell you the name of the parameter and routeData.Values.Values the value itself
Then, wherever you handle the exception, you can store the route data in a TempData variable, like this
TempData["RouteData"]
And after that, it will be available on your error page
#model System.Web.Mvc.HandleErrorInfo
#{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
#TempData["RouteData"];

Related

faces-redirect=true not working while creating and rendering view

I am currently working on a JSF 2.2 application. As per requirements, I have created custom view handler (using ViewHandlerWrapper) for my application. All the methods are just passing to default view handler except renderView which I am overriding as follows -
private viewHandler viewHandlerWrapped = null;
renderView(FacesContext facesContext, UIViewRoot viewToRender) {
String viewId = viewToRender.getViewId();
if (viewId == some condition) {
/* Do calculation to derive viewId */
}
UIViewRoot viewRoot = viewHandlerWrapped.createView(facesContext,viewId+"?faces-redirect=true");
facesContext.setViewRoot(viewRoot);
//now let system render the view
viewHandlerWrapped.renderView(facesContext,viewRoot);
}
The above is working fine and rendering & navigation is happening as expected. The only issue is faces-redirect=true is not working. The URL seems to be always one behind.
I have gone through many answers given in stackoverflow or internet. But nowhere I am able to find how to solve this.
I think I am doing something wrong e.g. ?faces-redirect=true might not be the correct way while creating view. But I am not sure what can be done to correct this.
Can someone please help me out with this?
After struggling with this for more than 4 weeks, I finally found a way to get the correct URL (instead of previous one). I am updating my answer here in case any one else falls into same problem -
"It looks like we can not use the faces-redirect=true the way I was using while creating and rendering the pages. It should be suffixed with form action. So I have changed my code as follows -
1) actions are returned on click of a button e.g.
public string doAction {
----
return "action?faces-redirect=true";
}
2) Code is updated to use implicit navigation wherever possible. With this, I didn't need to build my custom viewhandler as navigation is happening implicitly. So, I have scrapped the viewhandler.
With above two simple steps, the correct URL is being displayed on the browser now.

MVC Action Link Issue

I have come across what appears to be an inconsistency in MVC 5 regarding the Html.Actionlink. In different cshtml files I can use the same code, but the url target that is generated is different.
For example, this line of code:
<td>#Html.ActionLink(item.Description, "Edit", new { item.ParentTableID }) </td>
generates this URL
localhost\MyControllerClass\Edit?ParentTableID=35
That then properly calls the ActionView method Edit and feeds the parameter with 35 as expected.
However, in another cshtml file, this line
<td>#Html.ActionLink("Edit", "EditChild", new { id = f.ApplicationTableFieldID})</td>
produces this url
localhost/MyControllerClass/Edit/7
and when it hits the EditChild Action View, the parameter is null.
I have seen this now a couple of times and not yet been able to understand what makes the difference. But I need the first result.
Thanks.
Ensure that your ID parameters are named correctly in both your Action method and your ActionLink Html helper. The visual difference comes from MVC default routing and how it can take a parameter named ID and put it in the URL without the query string (? followed by stuff)
If your action method looks like this
public ActionResult EditChild(int ParentTableID){}
Then you will need to have your ID parameter named ParentTableID when you pass it back in your URL
<td>#Html.ActionLink("Edit", "EditChild", new { ParentTableID = f.ApplicationTableFieldID})</td>
Should now produce the following URL
localhost\MyControllerClass\EditChild?ParentTableID=3

How can I extract the main body of an Orchard Page via a Url

What I'm looking to do is have 2 views of an orchard page.
The first will include headers and footers, the second just the main body content.
The reason is so that we can maintain the body in one place, and it will be used either as a stand alone site, or just embedded within another.
I'm thinking that it would be done by accessing the page using a different route, or appending a querystring parameter.
Any other options are welcomed.
The method I am about to describe is arguably a hack and may go against some of the intentions of the creators of Orchard, but it will get the job done.
Orchard uses the ThemeFilter with the Themed attribute to decide whether the current controller action's output will be 'themed' --- i.e., be displayed with headers and footers. The controller used by Orchard to display content items has this attribute enabled, which is why content items are displayed themed. What you are asking to do is to suppress this 'themed' mode based on the presence of a query string parameter.
ThemeFilter kicks in at a very early stage of the page request and applies itself by setting a value in the current request's http context.
ThemeFilter.cs:
public static void Apply(RequestContext context) {
// the value isn't important
context.HttpContext.Items[typeof (ThemeFilter)] = null;
}
This class does not provide a way of unApplying this value. If you are willing to modify the Orchard source code, you may add this method yourself:
public static void Unapply(RequestContext context) {
context.HttpContext.Items.Remove(typeof (ThemeFilter));
}
Then you can simply create your own action filter that checks for the existence of a query string and then call this method if appropriate, something like:
using System.Web.Mvc;
using Orchard.Mvc.Filters;
using Orchard.Themes;
namespace Demo {
public class UnthemeFilter : FilterProvider, IActionFilter {
public void OnActionExecuting(ActionExecutingContext filterContext) {
}
public void OnActionExecuted(ActionExecutedContext filterContext) {
if (filterContext.RequestContext.HttpContext.Request["unthemed"] != null) {
ThemeFilter.Unapply(filterContext.RequestContext);
}
}
}
}
Now by adding ?unthemed=true, you will be able to suppress the theming.
If you are unwilling or unable to modify the Orchard source code, it is still possible to do the same thing by directly removing the typeof (ThemeFilter) from the HTTP context in your filter. However, this breaks encapsulation and should probably be avoided in object-oriented programming.

Preserving JSF messages after a redirect

I have a JSF page (using MyFaces 2.0) that does a bit of data gathering the first time it is displayed. If it can't find some information, it is supposed to provide a message to that effect and redirect back to a different page. I've tried using the solution found here Preserving FacesMessage after redirect for presentation through <h:message> in JSF (setKeepMessages(true)) but the messages aren't displaying after the redirect. The only difference I can pick out is that I'm not using a navigation rule, I'm calling the redirect() call on the external context because this isn't occurring in a normal action.
Relevant code:
public void redirectToPageWithMessage(String inPage, String message, FacesMessage.Severity severity){
getFlash().setKeepMessages(true);
addMessage(message, severity);
try {
getFacesContext().getExternalContext().redirect(inPage);
} catch (IOException e) {
e.printStackTrace();
}
}
Unfortunately this doesn't seem to be working. The redirect happens just fine, but the < messages /> tag isn't displaying the message. Is there something different about the way redirect() happens that prevents this from working?
The code that saves the messages are executed after the phase ends (see Flash.doPostPhaseActions(FacesContext) ). So, it is expected it does not work, but maybe you can call Flash.doPostPhaseActions before the redirect. Note is not a "clean" solution, but it is possible.
I had the same problem and solve it not using ExternalContext.redirect() but to play with outcome for your actions.
That is to say, my action called by my buttons return a String (the outcome) which indicates the navigation rules to go to the next page. With that system, the messages are preserved.
JSFMessages are kept only for the processing of the actual request. A second request is made when using redirect, so the JSFMessages will be lost. The EL-Flash is a way to work around this. This example should work: http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/

ASP.NET MVC 2 JQuery POST not displaying the model state errors

I have been using asp.net mvc for a bit (but I'm still a beginer). I want to have the ability to update two views as a result of a jquery postback.
Basically I have a list and a details view. The details view is presented using a jquery popup (using jquery-UI popup). I only want to update the list if the details save is successful (i.e. there are no validation errors on the details view). However, if there are any validation errros in the details view, I want to update the details view so that the user sees the validation errors.
so I thought in my controller, I return a JsonResult instead of a View.
[HttpPost]
public ActionResult SavePersonInfo(Person p) {
if(ModelState.Valid) {
return View("PersonList");
}
return Json({Error = true, View = PartialView("PersonDetails", p)});
}
As you can see if there are no errors I return the person list view, but if there are any validation errors, I have return the details view. The reason that I'm returning a JsonResult is I need to tell my view there is an error so that the view (jquery) knows which section to update (as in whether to update the person list 'div' or the popup dialog 'div').
So, in my view, the jquery is as follows (please assume that there is a form for entering in the person details and "SubmitPersonForm();" function is called upon clicking on the "Save" button):
<script type="text/javascript>
$('#btnSave').click(function (event) {
onBegin();
$.ajax(
{
type: "POST",
url: "/Person/Save",
data: $('form').serialize(),
success: function (result) {
if(result.Error) {
$('#dvDetails').html($(result).View));
}
else {
$('#dvPersonList').html($result);
}
}
});
});
</script>
So the problem that I have now, is that when there is a validation error, I do see the correct, 'div' being updated, but I lose the asp.net mvc validation messages. I do not see any validation errors in red, as if ASP.NET MVC is completely ignored them. However, my ModelState does have those errros, just not displayed in the details view. I do have valication summary and Html.ValidationFor(m => ...) statements put in my details view.
Could someone tell me why I'm not seeing the validation errors? although I'm using a JSonResult, I do use the right property which is a valid view when I render the 'dvDetails'. Am I doing something that I'm not suppose to in asp.net mvc? Btw I'm using asp.net mvc2 RC with Visual Studio 2010 RC.
Thank you.
The Url does not match the Action name ... not sure if you maybe are calling the wrong Method on the controller ... just an idea. :-)
ASP.NET MVC code processes the response to display validation errors stored in the ModelState during a postback. Because you are using an Ajax Post, the postback code is not rebuilding the page and displaying the validation errors. If you want to display validation errors, you have to handle it yourself.
I've done it by passing the ModelState errors as an array in the response. I process my Ajax response to update the validation error placeholder elements in the form with corresponding error messages in the response.
A warning, though: the keys in the ModelState may have their case changed, so an element ID string in the form doesn't exactly match the string that's used for the key. ModelStateDictionary is case-insensitive, but DOM ID's are not.

Resources