-1

如果已经问过这个问题,但我有一个asp.net网站,并且我所有的页脚页面都存储在 Visual Studio 下

视图 > 页脚 > [页面名称]

当我单击页脚链接时,我的 URL 显示为:

http://www.mysite.co.uk/Views/Footer/testpage

我所追求的是从 URL 中删除“/Views/Footer”,所以它看起来像:

http://www.mysite.co.uk/testpage

我不知道该怎么做。有人可以给我一步一步的代码使用指南,以及把它放在哪里,这样它就可以做到这一点。

当我尝试双击我Global.asax的文件时,它会自动打开Global.asax.cs我怀疑也是错误的文件

4

2 回答 2

1

将 system.web.routing 的引用添加到项目

在配置中将 urlroutingmodule 添加到 http 模块:

    <configuration> 
   ... 

   <system.web> 
      ... 
      <httpModules> 
         ... 
         <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
      </httpModules>
   </system.web> 

   <system.webServer> 
      <validation validateIntegratedModeConfiguration="false"/> 
      <modules> 
         ... 
         <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
      </modules> 


      <handlers>
         ...
         <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </handlers>
      ... 
   </system.webServer>
</configuration>

在 global.asax 中定义路由:

void Application_Start(object sender, EventArgs e) 
{ 
   RegisterRoutes(RouteTable.Routes); 
} 

void RegisterRoutes(RouteCollection routes) 
{ 
   // Register a route for Categories/All 
   routes.Add( 
      "All Categories",
         new Route("Categories/All", new CategoryRouteHandler()) 
      );

   // Register a route for Categories/{CategoryName} 
   routes.Add( 
      "View Category",
      new Route("Categories/{*CategoryName}", new CategoryRouteHandler()) 
   );

   // Register a route for Products/{ProductName} 
   routes.Add( 
      "View Product",
      new Route("Products/{ProductName}", new ProductRouteHandler()) 
   );
}

创建路由处理程序类

public class ProductRouteHandler : IRouteHandler 
{ 
   public IHttpHandler GetHttpHandler(RequestContext requestContext) 
   { 
      string productName = requestContext.RouteData.Values["ProductName"] as string; 

      if (string.IsNullOrEmpty(productName)) 
         return Helpers.GetNotFoundHttpHandler(); 
      else 
      { 
         // Get information about this product 
         NorthwindDataContext DataContext = new NorthwindDataContext(); 
         Product product = DataContext.Products.Where(p => p.ProductName == productName).SingleOrDefault(); 

         if (product == null) 
            return Helpers.GetNotFoundHttpHandler(); 
         else 
         { 
            // Store the Product object in the Items collection 
            HttpContext.Current.Items["Product"] = product; 

            return BuildManager.CreateInstanceFromVirtualPath("~/ViewProduct.aspx", typeof(Page)) as Page; 
         } 
      } 
   } 
} 

创建处理请求的 asp.net 页面:

protected void Page_Load(object sender, EventArgs e) 
{ 
   dvProductInfo.DataSource = new Product[] { Product }; 
   dvProductInfo.DataBind(); 
} 

protected Product Product 
{ 
   get 
   { 
      return HttpContext.Current.Items["Product"] as Product; 
   } 
}

这是一个很好的工作参考,我过去在网络表单应用程序中使用过它,它就像一个魅力。

于 2015-08-07T15:07:14.757 回答
-1

如果你不使用 MVC,那么你可以实现一个 IHttpModule。Internet 上有几个关于如何执行此操作的指南,例如 Scott Guthrie 的此处:http ://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net

于 2015-08-07T14:21:52.810 回答