轉:http://www.cnblogs.com/hllive/p/6029763.html
由vs2013新建“web窗體應用程序”的網站,URL不顯示擴展名。那今天就實現該功能
1、首先打開vs2013>新建項目>如下圖選擇
2、選擇項目右擊>管理NuGet程序包>在搜索框輸入(FriendlyUrls),搜到后點安裝(接受協議后安裝完成):
3、注意閱讀readme.txt,在解決方案如圖:
4、打開RouteConfig(路由配置)和MVC里的路由一樣,把原來的代碼修改了,如圖:
修改后:
5、選擇項目右擊>添加>新建項>全局應用程序類(Global.asax)設置如下:
添加如下代碼是程序在啟動時執行該方法(需引用的自己引用)
protected void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); } protected void Application_Error(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { }
protected void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); } protected void Application_Error(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { }
6、新建一個測試web窗體(test.aspx)可以什么都不用寫,效果如下:
7、獲得url值:
注意!引用命名空間:
<%@ Import NameSpace="Microsoft.AspNet.FriendlyUrls" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="urlTest.test" %>
<%@ Import NameSpace="Microsoft.AspNet.FriendlyUrls" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
測試窗口
<% foreach (var segment in Request.GetFriendlyUrlSegments()) { %>
<p><%: segment %></p>
<% } %>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="urlTest.test" %> <%@ Import NameSpace="Microsoft.AspNet.FriendlyUrls" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> 測試窗口 <% foreach (var segment in Request.GetFriendlyUrlSegments()) { %> <p><%: segment %></p> <% } %> </form> </body> </html>
這些可以做在后台,效果如下: