gpt4 book ai didi

c# - 未为 *.aspx 文件触发 asp.net handler.ashx 文件中的 httphandler

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:13 24 4
gpt4 key购买 nike

我有一个基于 asp.net webform 的旧网站。我需要使用同一个站点将 url 更改为友好的,并保持对旧 url 的支持。

为此,我想到了使用 HTTP 处理程序,这样我就可以拦截旧的 url 并将它们重定向到具有新 url 的正确页面

示例

旧网址:/news.aspx?newsID=10

新网址:/news/10/title-of-the-news

使用处理程序文件 URLHandler.ashx 我想将页面重定向到正确的页面或重新创建新的 url。

到目前为止我做了什么

  1. create URLHandler.ashx 编写了重定向到新 url 的逻辑,或者说我创建了新 URL 并重定向了页面

  2. 在 we.config 中注册处理程序

当我首先运行 /news.aspx?newsID=10 时,这对我不起作用,它应该执行处理程序文件并根据 url 重定向到页面。

我不确定我是否做错了什么。

web.config

  <system.webServer>
<!-- Un Comment This Part on web server Un Comment This Part on web server -->
<handlers>
<add name="url-handler" verb="*" path="*.aspx" type="URLHandler" resourceType="Unspecified" />
<add name="ChartImg" path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>

<validation validateIntegratedModeConfiguration="false" />
</system.webServer>

SAMPLE 处理程序文件代码

<%@ WebHandler Language="C#" Class="URLHandler" %>

using System;
using System.Web;

public class URLHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
// context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
HttpResponse r = context.Response;

r.Write(context.Request.Url.AbsoluteUri);
}

public bool IsReusable {
get {
return false;
}
}

}

最佳答案

将代码放在 .cs 文件中。我已经复制了您的项目(代码在 .ashx 文件中)并收到以下错误:

Could not load type 'URLHandler'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

将代码放在 App_Code.cs 文件中会使其按预期工作。我怀疑这与以下事实有关:.ashx 文件中的代码的编译方式略有不同,以解释它响应特定 URL 的事实(URLHandler.ashx) 因为它是一个物理文件,而不是从 web.config 引用的类,这可能足以破坏类型的名称/位置,因此 IIS 可以在配置中引用时找不到它。

关于c# - 未为 *.aspx 文件触发 asp.net handler.ashx 文件中的 httphandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33497387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com