Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler class code not firing

I am learning to use Handlers in ASP.NET.

I added Web Form and changed the extension to "*.bspx".

I added a class with code like this:

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

I added below lines in web.config file:

<system.webServer>
    <handlers>
      <add verb="*" path="*.bspx" name="Uniquehandler"/>
    </handlers>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

The page "default.aspx" has a Link Button with postback URL set to open the above page.

<asp:LinkButton ID="lnk" PostBackUrl="~/DifferentPage.bspx" runat="server" Text="Bspx"></asp:LinkButton>

But when I click the above link button, it shows error:

The HTTP verb POST used to access path '/DifferentPage.bspx' is not allowed. 

** Edited ** Web.Config code

<?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>
like image 438
RKh Avatar asked Nov 01 '22 09:11

RKh


1 Answers

You have missed type in webconfig file ,

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

Here is the "App_Code" code means your class file folder name.

Edit :

Here is my solution file details

enter image description here

Here is my uniqueHanlder class code from App-data

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

Here is my default.aspx code

<%@ 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>

And this is my web.config file code

<?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>
like image 176
Ramesh Rajendran Avatar answered Nov 15 '22 07:11

Ramesh Rajendran