安装了 ASP.Net 4.0 的 Web 服务器,部署了 Web Pages 2.0 DLL bin。用 cshtml/razor 编写的页面,但路由不起作用。
当仅使用网页而不是完整的 MVC(我在 global.asax 中定义路由)时,服务器需要什么来激活路由?
现在我只能使用传统的 URL 和查询字符串调用我的页面。
任何指示表示赞赏。
最佳答案
Web 表单应用程序
Global.asax
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx");
}
MVC 应用程序
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
了解更多信息:
How to: Use Routing with Web Forms
ASP.NET Routing not working on IIS 7.0
Deploying ASP.NET MVC 3 to IIS 6
IIS URL Rewriting and ASP.NET Routing
希望这对您有帮助。
关于asp.net-mvc - 如果没有 global.asax,ASP.NET 网页的路由如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13936909/