ASP.NET MVC2 - 重定向到重写控制器中的 Url

发布于 2024-09-11 01:00:01 字数 2629 浏览 7 评论 0原文

我创建了一个 ControllerBase 类,它重写了默认 Controller 类的 Execute 方法。我这样做是为了检查 URL 并从我们的数据库中提取关联的页面。如果找不到该页面,我想重定向到特定的网址(“无效页面”)。除了重定向之外,一切正常。我尝试使用 Response.Redirect("invalid-page"),但它给了我一个空引用错误。还尝试了 requestContext.HttpContext.Response.Redirect("invalid-page"),但同样的错误。我猜它不想让您通过使用响应重定向来绕过 MVC 流程。由于覆盖实际上并不返回 ActionResult,所以我不能只返回 Redirect 操作。有人知道我如何在这里进行重定向吗?

这是我的 ControllerBase 类:

public class ControllerBase : Controller
{
    protected override void Execute(System.Web.Routing.RequestContext requestContext)
    {
        ViewData.Model = new DynamicPage{ LeftColumnCss = "bg5"};
        var friendlyUrl = (string)requestContext.RouteData.Values["friendlyUrl"];
        if (friendlyUrl != null && !friendlyUrl.Equals("invalid-page"))
        {
            var page = PageService.FetchByFriendlyUrl(null, (int?)PageCategoryType.MomMavens, friendlyUrl);

            if (page == null)
            {
                var archivedPage = PageArchiveService.FetchByFriendlyUrl(friendlyUrl);
                if (archivedPage != null)
                {
                    page = PageService.FetchById(archivedPage.PageID);
                    if (page != null)
                    {
                        requestContext.HttpContext.Response.Redirect(PageService.GetPageAbsoluteUrl(page));
                    }
                }
            }

            if (page == null)
            {
                //This is where I need to figure out how to do a redirect                   //requestContext.HttpContext.Response.Redirect("invalid-page");
                //base.Redirect("invalid-page");
                Response.Redirect("contact-us");
                return;
            }
            ((DynamicPage)ViewData.Model).CurrentPage = page;

            // Allow the page to override the left column class
            if (!string.IsNullOrEmpty(page.LeftColumnCssClasses))
            {
                var classes = page.LeftColumnCssClasses.Split(';');
                var index = SubSonic.Sugar.Numbers.Random(0, classes.Length);
                if (index == classes.Length)
                    index--;
                ((DynamicPage)ViewData.Model).LeftColumnCss = classes[index];
            }
        }

        base.Execute(requestContext);
    }

}

编辑: 不知何故,我设法通过将行:更改

if (friendlyUrl != null && !friendlyUrl.Equals("invalid-page"))

为...

if (!friendlyUrl.Equals("invalid-page"))

然后使用 requestContext.HttpContext.Response.Redirect("invalid-page") 来修复它,尽管这之前给了我一个错误。不知道为什么这会有所不同。不知何故,带有空白网址的页面绕过所有这些检查会导致问题。

I've created a ControllerBase class that overrides the Execute method of the default Controller class. I'm doing this to check the url and pull an associated page from our database. If the page isn't found, I want to redirect to a specific url ("invalid-page"). All is working except the redirect. I've tried to use Response.Redirect("invalid-page"), but it gives me a null reference error. Also tried requestContext.HttpContext.Response.Redirect("invalid-page"), but same error. I'm guessing it doesn't want to let you bypass the MVC process by using a response redirect. And since the override doesn't actually return an ActionResult, I can't just return a Redirect action. Anyone know how I could do a redirect here?

Here's my ControllerBase class:

public class ControllerBase : Controller
{
    protected override void Execute(System.Web.Routing.RequestContext requestContext)
    {
        ViewData.Model = new DynamicPage{ LeftColumnCss = "bg5"};
        var friendlyUrl = (string)requestContext.RouteData.Values["friendlyUrl"];
        if (friendlyUrl != null && !friendlyUrl.Equals("invalid-page"))
        {
            var page = PageService.FetchByFriendlyUrl(null, (int?)PageCategoryType.MomMavens, friendlyUrl);

            if (page == null)
            {
                var archivedPage = PageArchiveService.FetchByFriendlyUrl(friendlyUrl);
                if (archivedPage != null)
                {
                    page = PageService.FetchById(archivedPage.PageID);
                    if (page != null)
                    {
                        requestContext.HttpContext.Response.Redirect(PageService.GetPageAbsoluteUrl(page));
                    }
                }
            }

            if (page == null)
            {
                //This is where I need to figure out how to do a redirect                   //requestContext.HttpContext.Response.Redirect("invalid-page");
                //base.Redirect("invalid-page");
                Response.Redirect("contact-us");
                return;
            }
            ((DynamicPage)ViewData.Model).CurrentPage = page;

            // Allow the page to override the left column class
            if (!string.IsNullOrEmpty(page.LeftColumnCssClasses))
            {
                var classes = page.LeftColumnCssClasses.Split(';');
                var index = SubSonic.Sugar.Numbers.Random(0, classes.Length);
                if (index == classes.Length)
                    index--;
                ((DynamicPage)ViewData.Model).LeftColumnCss = classes[index];
            }
        }

        base.Execute(requestContext);
    }

}

EDIT:
Somehow I managed to fix it by changing the line:

if (friendlyUrl != null && !friendlyUrl.Equals("invalid-page"))

to...

if (!friendlyUrl.Equals("invalid-page"))

and then using requestContext.HttpContext.Response.Redirect("invalid-page"), even though that gave me an error before. Not sure why that made a difference. Somehow having pages with blank urls bypass all those checks caused a problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2024-09-18 01:00:01

您尝试过Server.Transfer()吗?

似乎您可以对 RouteValueDictionary 进行更改并且它会起作用,但我不记得它是否是只读集合。

Have you tried a Server.Transfer()?

It seems like you could make a change to the RouteValueDictionary and it would work, but I can't remember if it's an readonly collection.

长亭外,古道边 2024-09-18 01:00:01

设法通过稍微更改 if 语句来修复它。请参阅原始帖子中的编辑。仍然不完全确定出了什么问题。

Managed to fix it by changing the if statement slightly. See edit in original post. Still not completely sure what was wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文