Har*_*uri 28 configuration-files asp.net-mvc-4
能否详细解释一下,在MVC4中使用App_Start()文件夹?我看到这个文件夹实际上在以前版本的MVC中不可用.此文件夹中有5个文件.
Spo*_*ock 36
App_Start是另一个将ASP.NET MVC配置组合在一起的文件夹,它在以前版本的ASP.NET MVC中完成Global.asax.
ASP.NET MVC引入了越来越多的配置元素,此文件夹非常适合放置此配置.例如,MVC 5的新版本.配置(例如第三方登录提供程序)也放在此文件夹(in Startup.Auth.cs)中.
App_Start不是ASP.NET/IIS识别的ASP.NET特殊文件夹.您可以根据需要重命名该文件夹.这个名字只是一个惯例,比如App_GlobalResouces等.
以下是每个文件的一些信息和参考点.使用这些文件非常简单.我已经包含了一些可能有助于您了解更多内容的在线参考资料.
AuthConfig - 注册外部认证提供商.有关更多信息,请参见ASP.NET MVC 外部身份验证提供程
BundleConfig - 注册您的CSS和JS,以便它们可以捆绑和缩小.另请参见ASP.NET MVC:指南:捆绑和缩小.
WebApiConfig- 仅在使用Web API时适用.它可用于配置特定于Web API的路由,任何Web API设置和Web API服务.另请参阅配置ASP.NET MVC Web API 2
FilterConfig - 注册的全球过滤器.这些过滤器适用于所有操作和控制器.另请参见ASP.NET MVC 3:全局操作筛选器
RouteConfig - 你已经找到了信息.
Bhu*_*dey 14
Mvc4中引入了App_start文件夹.它包含各种配置文件,如:
App_start不是MVC中的特殊文件夹,也不是其中的类文件,这些只是具有不同应用程序配置(过滤,捆绑,路由等)的普通类文件,所有这些设置都在Global.asax.cs文件的Application_Start方法中注册.
这用于创建和注册CSS和JS文件的包.例如.jQuery,jQueryUI,jQuery验证,Modernizr和Site CSS ..
捆绑和缩小是通过减少对服务器的请求数量和减少所请求资产(例如CSS和JavaScript)的大小来改进请求加载时间的两种技术 .Microsoft为其提供了组件Microsoft.Web.Optimization
例如.让我们创建两个Bundles.一个用于样式(css),另一个用于脚本(javascript)
您可以通过
在BundleConfig.cs文件中调用BundleCollection类Add()方法分别为css和javascripts创建bundle .
步骤1:
创建样式包
Run Code Online (Sandbox Code Playgroud)bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.min.css", "~/Content/mystyle.min.css"));创建脚本包
Run Code Online (Sandbox Code Playgroud)bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery-1.7.1.min.js", "~/Scripts/jquery.validate.min.js"));
第2步:
上面的bundle在BundleConfig类中定义为:
Run Code Online (Sandbox Code Playgroud)public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { //Adding StyleBundle to BundleCollection bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.min.css", "~/Content/mystyle.min.css")); //Adding ScriptBundle to BundleCollection bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery-1.7.1.min.js", "~/Scripts/jquery.validate.min.js")); } }
第3步:
注册捆绑
所有bundle都在Global.asax的Application_Start事件中注册:
Run Code Online (Sandbox Code Playgroud)protected void Application_Start() { BundleConfig.RegisterBundles(BundleTable.Bundles); }
缩小是一种从JavaScript和CSS文件中删除不必要的字符(如空格,换行符,制表符)和注释以减小大小,从而缩短网页加载时间的技术.例如.jquery-1.7.1.min.js是jquery-1.7.1的缩小的js文件,主要用于生产环境,对于非prod,你可以更好地使用非缩小的js来获得更好的可读性.
例如.
未压缩的js中的Jquery函数可能类似于:
( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
Run Code Online (Sandbox Code Playgroud)
压缩或缩小js中相同的上述函数将如下所示:
!function(a,b){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}
这用于创建和注册全局MVC过滤器:
例如.
注意:如上所述,过滤器按顺序执行.
例如.MVC5引入的身份验证过滤器:
Run Code Online (Sandbox Code Playgroud)public interface IAuthenticationFilter { void OnAuthentication(AuthenticationContext filterContext); void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext); }您可以通过实现
IAuthenticationFilter 来创建CustomAuthentication过滤器属性,如下所示 -
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
//logic goes here
}
Run Code Online (Sandbox Code Playgroud)
在OnAuthentication方法之后运行
public void OnAuthenticationChallenge(AuthenticationChallengeContext
filterContext)
{
{
//logic goes here
}
}
Run Code Online (Sandbox Code Playgroud)
配置过滤器
您可以在以下三个级别中将自己的自定义过滤器配置到应用程序中:
全球水平
通过将过滤器注册到Global.asax.cs文件的Application_Start事件中:
Run Code Online (Sandbox Code Playgroud)protected void Application_Start() { FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); }控制器级别
通过将过滤器放在控制器名称的顶部:
Run Code Online (Sandbox Code Playgroud)[Authorize(Roles="Admin")] public class AdminController : Controller { // Logic goes here }行动水平
通过将过滤器放在操作名称的顶部:
Run Code Online (Sandbox Code Playgroud)public class UserController : Controller { [Authorize(Users="User1,User2")] public ActionResult LinkLogin(string provider) { // Logic goes here return View(); } }
这用于为您的Asp.Net MVC
应用程序注册各种路由模式.路由在ASP.NET MVC应用程序执行流程中起着重要作用,它使用路由表将请求URL映射到特定的控制器操作.我们可以为引擎定义路由规则,以便它可以将传入的URL映射到适当的控制器.路由引擎使用Global.asax文件中定义的路由规则来解析URL并找出相应控制器的路径.我们可以在Global.asax文件的Application_Start()方法中找到以下代码.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Run Code Online (Sandbox Code Playgroud)
我们可以在App_Start文件夹下找到RouteConfig.cs文件.如果我们在RouteConfig类中遵循此方法,我们将找到一个默认配置的路由,如下所示.第3行到第7行正在配置一个默认路由.
Run Code Online (Sandbox Code Playgroud)public static void RegisterRoutes(RouteCollection routes) { 1. routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); 2. 3. routes.MapRoute( 4. name: “Default”, 5. url: “{controller}/{action}/{id}”, 6. defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } 7. ); }第4行:路线的名称.第5行:代表URL:Controller,动作后跟id(如果有的话).第6行:默认控制器为Home,默认操作为Index,Id为可选.
这用于注册各种WEB API路由,如Asp.Net MVC,以及设置任何addtional WEB API配置设置.
用于注册外部身份验证提供程序,例如.如果您希望用户使用来自外部提供商(如Facebook,Twitter,Microsoft或Google)的凭据登录,然后将这些提供商的某些功能集成到您的Web应用程序中.
| 归档时间: |
|
| 查看次数: |
57897 次 |
| 最近记录: |