2013-12-01 6 views
5

ASP.NET에서 처리기를 사용하는 방법을 배우고 있습니다.처리기 클래스 코드가 실행되지 않습니다.

Web Form을 추가하고 확장명을 "* .bspx"로 변경했습니다. 내가 web.config 파일의 줄 아래에 추가

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; } 
     } 
    } 
} 

:

나는 다음과 같은 코드를 가진 클래스를 추가

페이지가 "의 default.aspx는"포스트 백에 링크 버튼이 있습니다
<system.webServer> 
    <handlers> 
     <add verb="*" path="*.bspx" name="Uniquehandler"/> 
    </handlers> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

위의 페이지를 열도록 설정된 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. 

** 편집 ** 당신은 webconfig에 type을 놓친

<?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, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, 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> 
+0

는 "bspx"또는 "영문"을 확인하시기 바랍니다 내 web.config 파일의 코드? –

+0

의도적으로 .bspx 페이지를 처리 ​​할 처리기를 만들려면 .bspx를 완료해야합니다. – RKh

+0

'UniqueHandler'는 실제로 네임 스페이스를 가지고 있지 않습니까? 있다면, 완전한 이름을'web.config'에 넣어야합니다. –

답변

2

의 Web.config 코드 파일,

<add verb="*" path="*.bspx" type="UniqueHandler, App_Code" name="Uniquehandler"/> 

"App_Code"코드는 클래스 파일 폴더 이름을 의미합니다.

편집 : 여기

여기

enter image description here

는 응용 프로그램 데이터에서 내 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> 

는 그리고이

<?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> 
+0

위에서 언급 한 것과 같은 오류가 발생했습니다. – RKh

+0

Nope 그것은 나에게 효과가있다! App-Code 폴더에있는 'UniqueHandler'? –

+0

메일 ID를 알려주시겠습니까? 샘플 응용 프로그램을 보내 드리겠습니다. –

관련 문제