JCC*_*CCV 7 c# asp.net-mvc design-patterns global-asax asp.net-mvc-4
我从来没有遇到过这种代码模式.有人愿意向我解释一下吗?(或者这里有一个模式吗?有没有理由为什么这样做?这给了什么好处?)我是一般编程新手,这对我来说非常有趣:
的Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
//...
RouteConfig.RegisterRoutes(RouteTable.Routes);
//...
}
Run Code Online (Sandbox Code Playgroud)
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以轻松地在示例中编写代码,如下所示:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional });
}
Run Code Online (Sandbox Code Playgroud)
但是,随着必要配置量的增加,将其拆分为几个逻辑相关的块是有意义的.ASP.NET MVC支持这一点相当不错,默认项目模板只是试图引导您这样做.
| 归档时间: |
|
| 查看次数: |
4346 次 |
| 最近记录: |