webresource.axd文件的配置及使用

时间:2023-03-09 08:51:07
webresource.axd文件的配置及使用

今天看到同事的代码中使用到了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>
<!--、直接加载-->
<script src="test.axd"></script>
<script>
<!--、单击事件加载-->
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