【问题标题】:Asp.net - Remove Folder Structure From My URLAsp.net - 从我的 URL 中删除文件夹结构
【发布时间】:2015-10-30 23:52:19
【问题描述】:

抱歉,如果这已经被问过,但我有一个 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 文件

【问题讨论】:

  • 这在 MVC 中很容易做到,但我会用谷歌 URL 重写。
  • @Cyber​​drew 当我尝试打开我的Global.asax 文件时,它会自动打开Global.asax.cs 文件,因为我刚刚添加到我的帖子中,所以我该怎么做,因为我找到的所有帮助说我需要这样做
  • 你的应用是 webforms 还是 MVC?对于 MVC,注册路由非常容易。
  • @TGarrett 这是网络表单

标签: c# asp.net navigateurl


【解决方案1】:

如果你不使用 MVC,那么你可以实现一个 IHttpModule。互联网上有几个关于如何做到这一点的指南,例如 Scott Guthrie 的这里:http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net

【讨论】:

    【解决方案2】:

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

    将urlroutingmodule添加到config中的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; 
       } 
    }
    

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

    【讨论】:

      猜你喜欢
      • 2012-09-22
      • 1970-01-01
      • 2021-12-06
      • 2014-01-22
      • 2016-10-08
      • 2014-04-22
      • 2016-08-19
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多