如果已经询问过,但我有一个asp.net
网站并且我的所有页脚页面都存储在Visual Studio
观看次数>页脚> [页面名称]
当我点击页脚链接时,我的网址显示为:
http://www.mysite.co.uk/Views/Footer/testpage
我删除" / Views / Footer"之后的内容。来自URL,所以它像:
http://www.mysite.co.uk/testpage
我不知道该怎么做。有人可以给我一步一步指导使用代码以及放置它的位置,以便它能够做到这一点。
当我尝试双击我的Global.asax
文件时,它会自动打开我怀疑也是错误的Global.asax.cs
文件
答案 0 :(得分:1)
将对system.web.routing的引用添加到项目
在config:
中将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;
}
}
这是一个很好的参考工作,我过去在webforms应用程序中使用它,它就像一个魅力。
答案 1 :(得分:-1)
如果您没有使用MVC,那么您可以实现IHttpModule。互联网上有几个关于如何做到这一点的指南,例如Scott Guthrie在这里:http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net