ASP.NET MVC2父控制器不重定向 [英] ASP.NET MVC2 Parent Controller Not Redirecting

查看:94
本文介绍了ASP.NET MVC2父控制器不重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用父控制器向围绕应用程序使用的设置特定变量的ASP.NET应用程序MVC2。我也实现了验证,以确保在URI的ID存在于数据库中。如果不是这样,我重定向和停止脚本的执行。

I have an ASP.NET MVC2 application that uses a parent controller to setup specific variables that are used around the app. I also implement validation to make sure that an ID in the URI exists in the database. If it does not, I redirect and stop the execution of the script.

我的父控制器看起来是这样的:

My parent controller looks something like this:


// Inside class declaration

// Set instance of account object to blank account
protected Account account = new Account();

protected override void Initialize(System.Web.Routing.RequestContext requestContext) {
    // Call parent init method
    base.init(requestContext);

    // Check to make sure account id exists
    if (accountRepos.DoesExistById(requestContext.RouteData.Values["aid"].ToString()) {
        account = accountRepos.GetById(requestContext.RouteData.Values["aid"].ToString());
    } else {
        requestContext.HttpContext.Response.Redirect("url");
        requestContext.HttpContext.Response.End();
    }
}

起初,这个工作,但现在输入了不正确的ID时,不会重定向并使用账户类时抛出一个NullPointerException异常。我原来只是声明的账户变量,而实例化,但也证明抛出异常,并没有重定向。

At first this worked, but now when an incorrect id is entered, it doesn't redirect and throws a NullPointerException when the Account class is used. I originally just declared the account variable rather instantiating it, but that also proved to throw exceptions and didn't redirect.

我试图结束脚本执行的原因是因为我想确保它即使重定向不工作停止。有点儿像调用PHP头()后退出():P。如果我这样做不对,我就AP preciate任何指针。

The reason I try to end the execution of the script is because I want to make sure that it stops even if the redirect doesn't work. Kinda like calling exit() after header() in PHP :p . If I am doing this wrong, I would appreciate any pointers.

我只是想知道我怎么能解决这个问题。

I'm just wondering how I can fix this.

任何帮助是极大AP preciated = D

Any help is greatly appreciated =D

推荐答案

我不认为这是应该做你想要的正确方法。相反,你应该使用在你的路由的路由约束,以确保该ID的存在,和下降从那里回来一网打尽的路子。

I don't think that's the proper way to do what you want. Instead you should use route constraints on your routes to make sure the id exists, and fall back from there in a "catch all" route.

事情是这样的:

Routes.MapRoute("Name", "Url", new { ... }, new {
    Id = new IdConstraint() // <- the constraint returns true/false which tells the route if it should proceed to the action
});

的约束将是这样的:

The constraint would be something like this:

public class IdConstraint : IRouteConstraint {
    public bool Match(
        HttpContextBase Context,
        Route Route,
        string Parameter,
        RouteValueDictionary Dictionary,
        RouteDirection Direction) {
        try {
            int Param = Convert.ToInt32(Dictionary[Parameter]);

            using (DataContext dc = new DataContext() {
                ObjectTrackingEnabled = false
            }) {
                return (dc.Table.Any(
                    t =>
                        (t.Id == Param)));
            };
        } catch (Exception) {
            return (false);
        };
    }
}

这是我和我的路由使用,以确保我收到确实存在的ID。如果它不存在,则该约束返回一个假的,并且路由不执行与该请求继续向下的路线连锁。在你的路由的最底部,你应该有一个通用的捕捉,发送你的用户的页面,告诉他们,他们想不存在,做X或X(沿着这些线路的东西什么都的路线,我只是来了与场景)。

This is what I use with my routes to make sure that I'm getting an Id that really exists. If it doesn't exist, the constraint returns a false, and the route does not execute and the request continues down the route chain. At the very bottom of your routes you should have a generic catch all route that sends your user to a page that tells them what they want doesn't exist and to do X or X (something along those lines, I'm just coming up with scenarios).

这篇关于ASP.NET MVC2父控制器不重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆