asp.net – Handler类代码没有触发
发布时间:2020-12-16 03:23:19 所属栏目:asp.Net 来源:网络整理
导读:我正在学习在ASP.NET中使用Handler. 我添加了Web Form并将扩展名更改为“* .bspx”. 我添加了一个类,代码如下: namespace Experiments1 { public class UniqueHandler: IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response
我正在学习在ASP.NET中使用Handler.
我添加了Web Form并将扩展名更改为“* .bspx”. 我添加了一个类,代码如下: namespace Experiments1 { public class UniqueHandler: IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request.RawUrl.Contains(".bspx")) { string newUrl = context.Request.RawUrl.Replace(".bspx",".aspx"); context.Server.Transfer(newUrl); } } public bool IsReusable { get { return false; } } } } 我在web.config文件中添加了以下行: <system.webServer> <handlers> <add verb="*" path="*.bspx" name="Uniquehandler"/> </handlers> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> 页面“default.aspx”有一个链接按钮,其中设置了回发URL以打开上面的页面. <asp:LinkButton ID="lnk" PostBackUrl="~/DifferentPage.bspx" runat="server" Text="Bspx"></asp:LinkButton> 但是当我点击上面的链接按钮时,它显示错误: The HTTP verb POST used to access path '/DifferentPage.bspx' is not allowed. **已编辑** <?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application,please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/> <add name="LearnConnectionString" connectionString="Data Source=.;Initial Catalog=Learn;Integrated Security=True" providerName="System.Data.SqlClient"/> <add name="LearnConnectionString2" connectionString="Data Source=.sqlexpress;Initial Catalog=Learn;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Design,Version=4.0.0.0,Culture=neutral,PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Web.Extensions.Design,PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Windows.Forms,PublicKeyToken=B77A5C561934E089"/> </assemblies> </compilation> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880"/> </authentication> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/> </providers> </membership> <profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> </profile> <roleManager enabled="false"> <providers> <clear/> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/> </providers> </roleManager> </system.web> <system.webServer> <handlers> <add verb="*" path="*.bspx" type="UniqueHandler,App_Code" name="Uniquehandler"/> </handlers> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> 解决方法
您错过了webconfig文件中的类型,
<add verb="*" path="*.bspx" type="UniqueHandler,App_Code" name="Uniquehandler"/> 这里的“App_Code”代码表示您的类文件夹名称. 编辑: 这是我的解决方案文件详情 这是我在App-data中的uniqueHanlder类代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for UniqueHandler /// </summary> public class UniqueHandler : IHttpHandler { public UniqueHandler() { // // TODO: Add constructor logic here // } public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request.RawUrl.Contains(".bspx")) { string newUrl = context.Request.RawUrl.Replace(".bspx",".aspx"); context.Server.Transfer(newUrl); } } } 这是我的default.aspx代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="UsernameTextBox" Text="xxx" runat="server"></asp:TextBox> <asp:LinkButton ID="lnk" PostBackUrl="~/Default2.bspx" runat="server" Text="Bspx"></asp:LinkButton> </div> </form> </body> </html> 这是我的web.config文件代码 <?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application,please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5"/> <httpRuntime targetFramework="4.5"/> </system.web> <system.webServer> <handlers> <add verb="*" path="*.bspx" **type="UniqueHandler,App_Code"** name="Uniquehandler"/> </handlers> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – VS2013不编译ASP.NET MVC5视图
- asp.net-web-api2 – 何时将刷新令牌传递给API
- ASP.NET:如何检测身份验证超时
- asp.net – FormsAuthentication:是否安全?
- asp.net-mvc – 安全地从MVC5项目中删除所有OWIN软件包,拥有
- asp.net-mvc – 如何获取ModelState.AddModel错误绑定到kno
- asp.net-mvc – 如何从模型中为ASP.NET MVC DropDownList设
- asp.net-mvc – MVC不是json序列化实现IEnumerable的模型上
- asp.net-mvc – ASP.NET MVC:如何创建一个可用的UrlHelper
- asp.net-mvc – 从Scripts.Render中排除的Asp.Net MVC4捆绑