我在App_Code上创建了一个RazorFunctions.cshtml文件
@functions {
public static string GetActiveClassIf(string controllerName, string actionName = null)
{
var routeData = @HttpContext.Current.Request.RequestContext.RouteData;
string currentController = routeData.Values["controller"].ToString();
string currentAction = routeData.Values["action"].ToString();
return controllerName == currentController &&
(String.IsNullOrEmpty(actionName) || currentAction == actionName) ? "active" : "";
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,它给我2个错误(编译获得成功和站点工作没有问题)但错误很烦人.
RazorFunctions.cshtml作为内容(尝试编译但当然不能与cshtml文件一起使用)
Global.asax.cs是:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelMetadataConfig.RegisterModelMetadata();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
//Database.SetInitializer(new DropCreateDatabaseAlways<ApplicationDbContext>());
}
}
Run Code Online (Sandbox Code Playgroud)
和
<%@ Application Codebehind="Global.asax.cs" Inherits="Bouron.Web.MvcApplication" Language="C#" %>
Run Code Online (Sandbox Code Playgroud)
这是我第一次使用App_Code,所以我不知道还能做什么,所有搜索返回ASP.NET 1-2的结果,是剃刀甚至不存在的过时所以我不知道怎么可以我解决了这个问题
我正在使用Visual Studio 2015(以防万一)
我正在尝试设置我的依赖注入,我需要IAuthenticationManager从ASP.NET身份注入一个OwinContext.
为此,我来自我的Global.asax -> ServiceConfig.Configure()跑步:
container.Register(() => HttpContext.Current.GetOwinContext().Authentication);
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的应用程序时,我收到此消息:
在上下文中找不到owin.Environment项
为什么这个HttpContext.Current.GetOwinContext()不能从Global.asax获得?
Startup.cs
[assembly: OwinStartupAttribute(typeof(MyApp.Web.Startup))]
namespace Speedop.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.Auth.cs
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager<User, int>, User, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
getUserIdCallback: (id) => (Int32.Parse(id.GetUserId())) …Run Code Online (Sandbox Code Playgroud) 我试图创建一个干净的ASP.NET Web API服务,如果可能的话,不引用MVC程序集.我关注了这个博客http://www.codeproject.com/Articles/615805/Creating-a-Clean-Minimal-Footprint-ASP-NET-WebAPI 但是在Global.asax中使用这一行我还是要导入System.Web. Mvc组装.如果我将其删除,会对我的网络API服务产生影响吗?我尝试在没有它的情况下在我的本地运行我的服务,我没有遇到任何错误.
protected void Application_Start()
{
//AreaRegistration.RegisterAllAreas(); do we still need this?
WebApiConfig.Register(GlobalConfiguration.Configuration);
HandlerConfig.RegisterGlobalHandlers(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalConfiguration.Configuration.Filters);
}
Run Code Online (Sandbox Code Playgroud) 我需要运行的应用程序开始之前的代码和到现在为止我已经使用WebActivator要做到这一点,但如果我理解正确的OwinStartupAttribute是更适合于这一点,但对我来说,问题是,它运行后Application_Start在Global.asax,这是正确的或我可以配置它运行预应用程序启动?