ASP.NET 路由实现去除aspx后缀
2017-10-26 10:47
357 查看
源码下载
引用dll(DownLoad)
DotNetOpenAuth.Core.dll
Microsoft.AspNet.FriendlyUrls.dll
Microsoft.AspNet.Membership.OpenAuth.dll
Web.Config加入配置
Global.asax加入配置
新建RouteConfig.cs
ASP.NET 扩展=>去除asmx后缀
protected void Application_BeginRequest(object sender, EventArgs e)
{
/*# ASP.NET 实现去除asmx后缀 #*/
if (Request.Url.LocalPath.Contains(".asmx"))
this.MapPageRoute("/api/apiCenter", "/api/apiCenter.asmx");
/*# ASP.NET 实现去除asmx后缀 #*/
}
/*
* Web.Config加入(禁止直接访问asmx,只允许Post、Get、Soap请求asmx)
<system.web>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
<wsdlHelpGenerator href="拒绝访问.aspx"/>
</webServices>
</system.web>
*/
/// <summary>
/// URL重写(区分大小写)
/// </summary>
/// <param name="VirtualPath">虚拟路径(区分大小写)</param>
/// <param name="RealPath">真实路径(区分大小写)</param>
private void MapPageRoute(string VirtualPath, string RealPath)
{
string LocalPath = Request.Url.LocalPath;
if (!LocalPath.Contains(RealPath))
{
if (LocalPath.Contains(VirtualPath))
{
Context.RewritePath(LocalPath.Replace(VirtualPath, RealPath));
}
}
}
引用dll(DownLoad)
DotNetOpenAuth.Core.dll
Microsoft.AspNet.FriendlyUrls.dll
Microsoft.AspNet.Membership.OpenAuth.dll
Web.Config加入配置
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <!--# ASP.NET 实现去掉aspx后缀 #--> <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" /> <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" /> <!--# ASP.NET 实现去掉aspx后缀 #--> </dependentAssembly> </assemblyBinding> </runtime> </configuration> <!--ProjectGuid: 5ACDD4E8-3966-4E26-9538-B62996B5E60F-->
Global.asax加入配置
protected void Application_Start(object sender, EventArgs e) { /*# ASP.NET 实现去掉aspx后缀 #*/ //扩展自定义路由 //var defaults = new RouteValueDictionary { { "name", "*" }, { "id", "*" } }; //RouteTable.Routes.MapPageRoute("", "employees/{name}/{id}", "~/EmployeePage.aspx", true, defaults);//defaults可为null //页面中读取路由值 //string name = Page.RouteData.Values["name"].ToString() //string id = Page.RouteData.Values["id"].ToString() RouteConfig.RegisterRoutes(RouteTable.Routes);//官方aspx路由规则 /*# ASP.NET 实现去掉aspx后缀 #*/ }
新建RouteConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; /*# ASP.NET 实现去掉aspx后缀 #*/ using Microsoft.AspNet.Membership.OpenAuth; using System.Web.Routing; using Microsoft.AspNet.FriendlyUrls; /*# ASP.NET 实现去掉aspx后缀 #*/ namespace AuthStore { public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.EnableFriendlyUrls(); } } }
ASP.NET 扩展=>去除asmx后缀
protected void Application_BeginRequest(object sender, EventArgs e)
{
/*# ASP.NET 实现去除asmx后缀 #*/
if (Request.Url.LocalPath.Contains(".asmx"))
this.MapPageRoute("/api/apiCenter", "/api/apiCenter.asmx");
/*# ASP.NET 实现去除asmx后缀 #*/
}
/*
* Web.Config加入(禁止直接访问asmx,只允许Post、Get、Soap请求asmx)
<system.web>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
<wsdlHelpGenerator href="拒绝访问.aspx"/>
</webServices>
</system.web>
*/
/// <summary>
/// URL重写(区分大小写)
/// </summary>
/// <param name="VirtualPath">虚拟路径(区分大小写)</param>
/// <param name="RealPath">真实路径(区分大小写)</param>
private void MapPageRoute(string VirtualPath, string RealPath)
{
string LocalPath = Request.Url.LocalPath;
if (!LocalPath.Contains(RealPath))
{
if (LocalPath.Contains(VirtualPath))
{
Context.RewritePath(LocalPath.Replace(VirtualPath, RealPath));
}
}
}
相关文章推荐
- asp.net mvc 5.0 借助路由规则实现*.aspx与HttpHandler交互
- 应用Ajax.net在Asp.net中实现无aspx文件应用
- 在 ASP.NET Core 项目中实现小写的路由URL
- Asp.net 路由系统的实现
- 在ASP.NET MVC中通过URL路由实现对多语言的支持
- 应用Ajax.net在Asp.net中实现无aspx文件应用
- ASP.NET 路由实现页面静态化
- 剖析Asp.Net路由系统实现原理
- ASP.NET MVC自定义路由 - 实现IRouteConstraint限制控制器名(转载)
- AspNet Mvc 路由解析中添加.html 等后缀 出现404错误的解决办法
- ASP.NET MVC 自定义后缀路由设置
- ASP.NET 路由实现页面静态化
- HttpModule 实现 ASP.Net (*.aspx) 中文简繁体的自动转换,不用修改原有的任何代码,直接部署即可!
- ASP.NET MVC 自定义后缀路由设置
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- ASP.NET路由系统实现原理:HttpHandler的动态映射
- asp.net不用设置iis实现url重写 类似伪静态路由
- ASP.NET MVC 多语言实现——URL路由
- asp.net 二级域名(路由方式实现)