[英]httphandler in asp.net handler.ashx file not triggered for *.aspx file
我有一個基於 asp.net webform 的舊網站。 我需要使用相同的站點將 url 更改為友好,並保持對舊 url 的支持。
為此,我想到使用 HTTP 處理程序,以便我可以攔截舊 url 並將它們重定向到帶有新 url 的正確頁面
例子
old url: /news.aspx?newsID=10
new url: /news/10/title-of-the-news
使用處理程序文件URLHandler.ashx
我想將頁面重定向到正確的頁面或重新創建新的 url。
到目前為止我做了什么
創建URLHandler.ashx
編寫了重定向到新 URL 的邏輯,或者讓我們說我創建了新 URL 並重定向了頁面
在 we.config 中注冊處理程序
當我首先運行/news.aspx?newsID=10
時,這對我/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
文件中的代碼)並收到以下錯誤:
無法加載類型“URLHandler”。
說明:在執行當前 Web 請求期間發生未處理的異常。 請查看堆棧跟蹤以獲取有關錯誤及其在代碼中的來源的更多信息。
將代碼放在App_Code
的.cs
文件中會導致它按預期工作。 我懷疑這與.ashx
文件中的代碼的編譯方式略有不同,以解釋它響應特定 URL ( URLHandler.ashx
) 的事實,因為它是一個物理文件,而不是一個類從web.config
引用,這可能足以破壞類型的名稱/位置,因此 IIS 在 config 中引用時找不到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.