码迷,mamicode.com
首页 > Web开发 > 详细

webresource.axd文件的配置及使用

时间:2014-11-18 00:22:22      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   使用   sp   

今天看到同事的代码中使用到了webresource.axd,特地认真地看了一下它的使用。主要用途有两点:

1、当作httphandler用,但是比handler更好用一点,不需要考虑路径,用的时候,只要名称一致就行。

2、用于内嵌js、css等资源文件使用。

首先说第一种使用:

Webconfig文件:

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
   
    <httpHandlers>
      <!--注册test.axd-->
      <add verb="*" path="test.axd" type="JHSoft.Example.UILogic.TestAxd,JHSoft.Example.UILogic"/>
    </httpHandlers>
  </system.web>
  <connectionStrings>
    <add name="BuxiyuanEntities" connectionString="metadata=res://*/BuxiyuanModel.csdl|res://*/BuxiyuanModel.ssdl|res://*/BuxiyuanModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=2012-20140730VY;initial catalog=Buxiyuan;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

 

页面代码:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="JHSoft.Example.UILogic.TextBoxSimple" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://cdn.renzaijianghu.com/Static/Script/jquery-1.9.1.js"></script>
    <!--1、直接加载-->
    <script src="test.axd"></script>
    <script>
        <!--2、单击事件加载-->
    function testAxd() {
        $.ajax({
            type: "post", //要用post方式                 
            url: "test.axd",//test.axd与webconfig中的path的值一样
            async: false,
            success: function (data) {

                if (data) {
                    alert(data);   //结果:弹出HelloWorld!
                }

            },
            error: function (err) {
                alert(err);
            }
        });

        //$.post("test.axd", function (result) {

        //    alert(result);
        //});
 
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
             
            <input type="button" value="testAxd" onclick="testAxd()" /> 
        </div>
    </form>
</body>
</html>
注意:
1)、代码中的<!--1、直接加载-->表示,我们可以通过
<script src="test.axd"></script>运行上面的页面时就可以访问到test.axd映射的JHSoft.Example.UILogic.TestAxd.cs类中。
2)、代码中的<!--2、单击事件加载-->表示,我们可以单击按钮触发事件,通过jquery的ajax异步进入JHSoft.Example.UILogic.TestAxd.cs类

下面再给出类文件JHSoft.Example.UILogic.TestAxd.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace JHSoft.Example.UILogic
{
    public class TestAxd : IHttpHandler
    {

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("Hello World!");
        }
    }
}

 第二种用法从网上学习得知:

 链接为:http://blog.csdn.net/heker2007/article/details/2078117

webresource.axd文件的配置及使用

标签:style   blog   http   io   color   ar   os   使用   sp   

原文地址:http://www.cnblogs.com/renzaijianghu/p/4104696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!