Tag: asp.net mvc 5

如何在基本的MVC5.1网站中正确注册AutoFac?

AutoFac最近已针对MVC 5.1进行了更新,但在撰写本文时,我发现缺少文档(特别是对于一个简单的示例)。 我想将依赖项注入MVC​​控制器并注册我自己的实现,例如电子邮件(实际发送与打印到输出窗口)作为基本示例。 我不知道我是否遗漏了一个很好的资源,我有点担心,因为它使用OWIN规范,实现可能与MVC5.1不同(ASP.NET身份使用OWIN并且使用了一些特殊属性正确实例化OWIN)所以需要检查我是否正确。 我有一个使用以下设置代码的工作示例 – 对于标准MVC5.1 Web应用程序,这是正确和良好的做法吗? 额外问题:我是否需要将InstancePerHttpRequest添加到RegisterControllers行? 即builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerHttpRequest(); (注意:我在Autofac上看到GitHub上的示例,但找不到适合MVC5.1的简单示例。) public static void RegisterDependencies() { // Register MVC-related dependencies var builder = new ContainerBuilder(); builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterModelBinders(typeof(MvcApplication).Assembly); builder.RegisterModule(); // Register e-mail service builder.RegisterType().As().InstancePerHttpRequest(); builder.RegisterModelBinderProvider(); // Set the MVC dependency resolver to use Autofac var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } 而我的控制器: public class HomeController : […]

使用MVC5和OWIN的自定义标识

我尝试使用MVC5和OWIN身份validation为Web站点的ApplicationUser添加自定义属性。 我已经阅读了https://stackoverflow.com/a/10524305/264607 ,我喜欢它与基本控制器的集成方式,以便于访问新属性。 我的问题是,当我将HTTPContext.Current.User属性设置为我的新IPrincipal时,我得到一个空引用错误: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs) +127 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69 这是我的代码: protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User.Identity.IsAuthenticated) { userManager = new UserManager(new UserStore(new ApplicationDbContext())); ApplicationUser user = userManager.FindByName(HttpContext.Current.User.Identity.Name); PatientPortalPrincipal newUser = new PatientPortalPrincipal(); newUser.BirthDate = […]

SignInManager,它是什么以及如何使用?

我正在探索SignInManager类。 但MSDN上提供的信息非常无用。 它只告诉提供的方法和属性。 我在寻找的是, 1)什么是SignInManager? 2)如何使用它? 3)我有自己的数据库,其中包含凭据相关信息(用户名和密码) 我如何使用SignInmanager以及如何使用它,以便我的自定义数据库用于validation用户? 我正在使用asp.net MVC 5和Visual Studio 2015.在我的示例项目中,我有包含操作方法的帐户控制器 public async Task ExternalLoginCallback(string returnUrl) 但我不知道如何使用它,MSDN完全无用提供这方面的信息。 任何有用的链接都会详细解释它,因为我不知道SignInManager是什么以及它的用途。 谢谢

默认MVC 5应用程序的安全例外

在将使用VS 2013生成的默认MVC 5应用程序部署到我的主机帐户时,我遇到了安全性exception。 有这个问题的快速解决方案吗? Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application’s trust level in the configuration file. Exception Details: System.Security.SecurityException: Request for the permission of type ‘System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed. […]

Enum RadioButtonFor Editor Template设置值

基于这个问题,我实现了一个RadioButtonFor编辑器模板。 我工作得很好,但目前你无法传递你想要的价值。 EnumRadioButtonList.cshtml (Editor Template): @model Enum @foreach (var value in Enum.GetValues(Model.GetType())) { if ((int)value > 0) { @Html.RadioButtonFor(m => m, (int)value) @Html.Label(value.ToString()) } } 我从View中调用它: @Html.EditorFor(m => m.QuestionResponse, “EnumRadioButtonList”) 如何传递值QuestionResponse(枚举)以便选择单选按钮?

project.Models.tableName:字段pvid必须是字符串或数组类型,最大长度为“20”

当我尝试在数据库中添加新条目时遇到以下exception错误: db.patient_visit.Add(pv); db.SaveChanges(); project.dll中出现“System.InvalidOperationException”类型的exception,但未在用户代码中处理 附加信息:project.Models.tableName:字段pvid必须是字符串或数组类型,最大长度为“20”。 在此之前,我的模型自动设置如下,因为我使用现有数据库进行了代码优先迁移。 [Key] [StringLength(20)] public string pvid { get; set; } 从逻辑上讲,它看起来已经在范围内了。 但是为了尝试,我尝试将其更改为下面的代码,并进行了添加迁移。 然后,在Up()方法中,它为空。 这是否意味着修改无法识别? 因为当我再次尝试运行项目时,我遇到了与上面相同的错误。 [Key] [MaxLength(20), MinLength(12)] public string pvid { get; set; } 然后,当我在我的查询中放置断点时,我看到: InnerException:null 消息:数据读取器与指定的“project.Models.tableName”不兼容。 “columnName”类型的成员在数据读取器中没有相应的具有相同名称的列。 我的查询有问题吗? ChargeVM objDetails = new ChargeVM(); var query3 = db.Database.SqlQuery(“SELECT MAX(CAST(SUBSTRING(pvid, 3, 10) AS int)) FROM patient_visit”); if (query3 != null) […]

如何解析参数对System.ComponentModel.PropertyChangingEventHandler无效

我正在重写MVC5中的ASP.NET MVC3应用程序,它大量使用Knockout和MVVM。 模型类充满了INotifyPropertyChanged和INotifyPropertyChanging函数。 将以下语句带入MVC5应用程序时,我收到上述主题行中的构建错误: public event PropertyChangingEventHandler PropertyChanging; public event PropertyChangedEventHandler PropertyChanged; protected virtual void SendPropertyChanging() { if (this.PropertyChanging == null) return; this.PropertyChanging((object)this, AFCity.emptyChangingEventArgs); } 必须采取哪些措施来解决上述错误?

仅当使用NetworkService时,站点才会使用ApplicationPoolIdentity在本地启动:“HTTP错误503.服务不可用”。

我希望从社区获得一些帮助,我开始提出这个问题,但在完全解释和详细说明我的问题的过程中,似乎我已经设法解决了它,似乎是一种合法的非hackish方式(请告诉我如果我错的话,所以我会把它放在这里,希望它会帮助别人。 题 我正在将Visual Studio 2010 .NET 4 MVC4站点升级到Visual Studio 2013 .NET 4.5 MVC5。 使用Visual Studio IIS时,该站点运行正常,但当我将其切换到Windows IIS并将用户设置为ApplicationPoolIdentity并启动该站点时,我得到一个页面,上面写着“HTTP错误503.服务不可用。”,以及ApplicationPool被停止了。 我发现有几个问题在StackOverflow上问同样的事情,但没有一个问题有可接受的解决方案。 一个答案是右键单击AppPool> AdvancedSettings> LoadUserProfile = FALSE。 这确实有用,我和它一起使用了一段时间,但我发现它导致了PhantomJS加载外部脚本的问题。 我通过将AppPoolUser设置为NetworkService并将LoadUserProfile设置为FALSE(仍然破坏了PhantomJS)进行了测试,然后将LoadUserProfile设置为TRUE并且它工作正常。 所以我使用NetworkService运行网站,这一切都很好,但我希望能够在完成此升级时使用ApplicationPoolIdentity,以便我可以在生产中使用ApplicationPoolIdentity。 我检查了EventViewer,我收到很多警告,告诉我无法复制文件 \\?\C:\Users\Default\AppData\Roaming\Microsoft\Templates\LiveContent\Managed\SmartArt 至 \\?\C:\Users\TEMP.IIS APPPOOL.004\AppData\Roaming\Microsoft\Templates\LiveContent\Managed\SmartArt Graphics\1033\TC101793893[[fn=Tabbed Arc]].glox. 我创建了一个名为APPPOOL.004的文件夹,然后它抱怨了APPPOOL.005,然后是APPPOOL.006,它就一直在继续。 解 然后,我将ApplicationPoolIdentity的完全权限授予C:\ Users文件夹,再次启动该站点,一切正常,配置文件必须已正确创建,并且我的网站正常工作。 然后,我从ApplicationPoolIdentity中删除了对C:\ Users的访问权限,该网站仍然可以正常工作。

MVC上下文菜单丢失

我一直在关注这个教程http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started on MVC 5.我已经尝试添加一个控制器,当我右键单击控制器并选择“添加”,我没有得到图片中的一些选项 – 例如“Controller”或“New Scaffolded Item …”。 我正在使用VS 2013 Professional,并且已经卸载并重新安装,尝试修复安装,但都无济于事。 有没有人有任何想法? 提前致谢

API Application Insights使用的良好实践

我阅读了这篇文档: https : //docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics 跟踪exception,跟踪跟踪等有许多不同的API方法。 我有一个ASP.NET MVC 5应用程序。 例如,我有以下控制器方法(由ajax调用): [AjaxErrorHandling] [HttpPost] public async Task SyncDriverToVistracks(int DriverID) { if ([condition]) { // some actions here try { driver.VistrackId = await _vistracksService.AddNewDriverToVistrackAsync(domain); await db.SaveChangesAsync(); } catch (VistracksApiException api_ex) { // external service throws exception type VistracksApiException throw new AjaxException(“vistracksApiClient”, api_ex.Response.Message); } catch (VistracksApiCommonException common_ex) { // external […]