c# - ASP.NET MVC2 Parent Controller Not Redirecting -
I have the ASP.NET MVC2 application which will allow a parent controller to set up specific variables used around the app. uses it. I also apply the verification to ensure that an ID is present in the database in the URI, if it does not happen, then I redirect the script execution and stop it.
The controller of my parents looks like this:
// Set the inside class announcement // Example empty account account account protected account account = new Account (); Secure Override Zero initialize (System.Web.Routing.RequestContext requestContext) {// call original init method base.init (requestContext); // Be sure that the account id exists, check them if (accountRepos.DoesExistById (requestContext.RouteData.Values ["Help"]. ToString ()) {accounts = accountRepos.GetById (requestContext.RouteData.Values ["Help"]. ToString ());} else {requestContext.HttpContext.Response.Redirect ("uRL"); requestContext.HttpContext.Response.End ();}}
Firstly it worked, but now when a wrong ID has been entered, it does not redirect and throws a NullPointerException when the account class is used. I originally declared the variable variable but rather instantiating it, Nor did it prove to be an exception and did not redirect.
Because of this, I try to finish the execution of the script because I have to ensure that it stops, even if Redirection should not work, just like calling after the header () in PHP: If I
I'm just thinking how I can fix it.
Any help is greatly appreciated = D
I do not think this is the proper way to do what you want to do. Instead you should use the lack of route on your routes to make sure that the ID exists, and stay behind from there in the "Hold all" way.
Something like this:
Routes.MapRoute ("name", "url", new {...}, new {id = new idconstraint (// //) Lt; - Obstacle correct / incorrect return which tells the path to take action that it should proceed}};
The constraint will be something like this:
public class IdConstraint: IRouteConstraint {public bool match (HttpContextBase context, train root, string parameter, RouteValueDictionary dictionary, route Direction of direction) {try {int Param = Convert.ToInt32 (dictionary [parameter]); (Datacentex DC = New DataCentx () {ObjectTrackingEngined = False}} (Returns (DC Table. ANY (T => (TID == Ultimate)));}}} Grip (Exception) { Return (false);}}}}
This is the place I make sure to use it with my routes that I am actually getting an id present. If this does not exist, then the obstacle gives a wrong return, and the route is not executed The request continues down the root chain. At the very bottom of your routes you should have a normal route that sends your user to a page that tells them what they want and X or X (some of those lines, I'm just coming up with scenarios).
Comments
Post a Comment