ASP.NET 重定向不适用于 UIProcess 应用程序块的无效 URL - 正在寻找重定向架构的想法?

发布于 2024-12-14 18:16:52 字数 3091 浏览 1 评论 0原文

在我的 global.asax.vb 文件中,如果 URL 上有前缀,我有代码来重写 URL。我们正在我们的应用程序中引入一个新的上下文。所以每一页要么是上下文头发,要么是唾液。

在 ASP.NET 代码(堆栈)到达此全局代码之前,它会调用一个名为 UIProcess 的应用程序块。这是微软几年前编写的代码,现在不再受支持。 UIP 块有点模仿 MVC,并且您将所有视图、导航和控制器详细信息存储在 web.config 中。 UIP 块正在执行重定向,如下所示。请注意,他们有一个从未修复(注释掉)的已知错误,因此我必须在从 .NET 2.0 升级到 .NET 3.5 之前重新编译它。这就是我评论过的。这是我知道的唯一错误。

    private void RedirectToNextView(string previousView, ViewSettings viewSettings)
    {
        try
        {
            //if (previousView == null)
            //    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/" + viewSettings.Type, true);
            //else
            //    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/" + viewSettings.Type, false);
            if (previousView == null)
                HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + viewSettings.Type, true);
            else
                HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + viewSettings.Type, false);
        }
        catch (System.Threading.ThreadAbortException) { }
    }

这是 Global.asax.vb 代码: (同样,这段代码现在并不重要,因为它还没有到达这里,抛出异常)

Sub Application_BeginRequest(ByVal sender As Object, _
                             ByVal e As EventArgs)
    ' Fires at the beginning of each request
    Dim originalUri As Uri = Request.Url
    Dim rewrittenUrl As String = String.Empty

    'Rewrite Saliva and Hair Testing urls 
    Select Case True
        Case originalUri.AbsolutePath.StartsWith("/HairTest/")
            rewrittenUrl = originalUri.AbsolutePath.Remove(0, 9)

            If Not originalUri.Query.Contains("SampleTypeContext=247") Then
                rewrittenUrl += "?sampleTypeContext=247"
            End If
        Case originalUri.AbsolutePath.StartsWith("/SalivaTest/")
            rewrittenUrl = originalUri.AbsolutePath.Remove(0, 11)

            If Not originalUri.Query.Contains("SampleTypeContext=3301") Then
                rewrittenUrl += "?sampleTypeContext=3301"
            End If
    End Select

    If rewrittenUrl <> String.Empty Then
        'append the original query if there was one specified
        If originalUri.Query <> String.Empty Then
            If rewrittenUrl.Contains("?") Then
                rewrittenUrl += "&"
            Else
                rewrittenUrl += "?"
            End If
            rewrittenUrl += originalUri.Query.Remove(0, 1)
        End If
        Context.RewritePath(rewrittenUrl)
    End If
End Sub

当我尝试用“/”预先挂起我的 URL(上面的 viewSettings.Type 变量)时,应用程序实际上导致了上面的异常。 HairTest”或“/SalivaTest”。它会导致 System.Threading.ThreadAbortException。我这么想是因为该路径实际上并不存在于我们的 Web 应用程序中,但我只是猜测。请注意,我们正在全局中进行重写,而不是重定向。我们的重写在 URL 前面加上“/HairTest”或“/SalivaTest”。

我们的 Web 应用程序中的所有页面都期望“SampleTypeContext”参数(如果需要)。如果您能想到一种更适合这种情况的方法,请告诉我。我将尝试获取有关异常的更多详细信息。

寻找想法!如果我们遇到此 UIProcess 块的问题,我们的架构方法仍有待讨论。我们不能仅仅删除 UIP 块,因为它在我们的整个应用程序中使用,但如果需要,我可以修改上面的代码(在我的第一个代码片段中)。

In my global.asax.vb file, I have code to re-write the URL if there is a prefix on the URL. We are introducing a new context in our application. So every page will either be of context hair or saliva.

Before the ASP.NET code (stack) even reaches this Global code, it calls an application block called UIProcess. It's code that Microsoft wrote years ago, and is no longer supported. The UIP block sort of mimics MVC, and you store all views, navigation and controller details inside the web.config. The UIP block is doing a redirect as shown below. Note, they had a known bug that was never fixed (commented out), so I had to recompile it before upgrading from .NET 2.0 to .NET 3.5. That's what I have commented out. That's the only bug I'm aware of.

    private void RedirectToNextView(string previousView, ViewSettings viewSettings)
    {
        try
        {
            //if (previousView == null)
            //    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/" + viewSettings.Type, true);
            //else
            //    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/" + viewSettings.Type, false);
            if (previousView == null)
                HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + viewSettings.Type, true);
            else
                HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + viewSettings.Type, false);
        }
        catch (System.Threading.ThreadAbortException) { }
    }

Here is the Global.asax.vb code:
(again this code doesn't matter right now because it's not getting here YET with the exception being thrown)

Sub Application_BeginRequest(ByVal sender As Object, _
                             ByVal e As EventArgs)
    ' Fires at the beginning of each request
    Dim originalUri As Uri = Request.Url
    Dim rewrittenUrl As String = String.Empty

    'Rewrite Saliva and Hair Testing urls 
    Select Case True
        Case originalUri.AbsolutePath.StartsWith("/HairTest/")
            rewrittenUrl = originalUri.AbsolutePath.Remove(0, 9)

            If Not originalUri.Query.Contains("SampleTypeContext=247") Then
                rewrittenUrl += "?sampleTypeContext=247"
            End If
        Case originalUri.AbsolutePath.StartsWith("/SalivaTest/")
            rewrittenUrl = originalUri.AbsolutePath.Remove(0, 11)

            If Not originalUri.Query.Contains("SampleTypeContext=3301") Then
                rewrittenUrl += "?sampleTypeContext=3301"
            End If
    End Select

    If rewrittenUrl <> String.Empty Then
        'append the original query if there was one specified
        If originalUri.Query <> String.Empty Then
            If rewrittenUrl.Contains("?") Then
                rewrittenUrl += "&"
            Else
                rewrittenUrl += "?"
            End If
            rewrittenUrl += originalUri.Query.Remove(0, 1)
        End If
        Context.RewritePath(rewrittenUrl)
    End If
End Sub

The application is actually causing an exception above, when I try to pre-pend my URL (viewSettings.Type variable above) with "/HairTest" or "/SalivaTest". It causes that System.Threading.ThreadAbortException. I'm thinking because that path doesn't actually exist in our web application, but I'm just guessing. Notice, we're doing a re-write in our global, not a redirect. Our re-write prepends the URL with "/HairTest" or "/SalivaTest".

All of the pages in our web application expect that "SampleTypeContext" parameter if it needs it. If you can think of a way that will work better for this situation, let me know. I'll try to get more details on the exception.

Looking for ideas!! Our architecture approach is still up for discussion if we run into issues with this UIProcess block. We can't just get rid of the UIP block since it's used throughout our application, but I can modify the code above (in my first code snippet) if we need to.

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

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

发布评论

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

评论(1

断桥再见 2024-12-21 18:16:58

没有标准方法可以使用 UIP 块来执行此操作。微软不再维护它了。我重载了库中的一些方法以使其工作。这样我们就可以在每个 UIP 重定向调用上附加一个查询字符串参数 (applicationScope)。

如果您需要代码,请在此处添加评论,我会将源代码发送给您。

There's no standard way to do this with the UIP block. And Microsoft doesn't maintain it anymore. I overloaded a bunch of the methods in the library to make it work. So that we can append a query string parameter (applicationScope) on each of our UIP redirect calls.

If you need the code, add a comment here, and I'll send you the source.

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