草庐IT

c# - ASP.NET MVC 4,EF5,模型中的唯一属性 - 最佳实践?

ASP.NETMVC4、EF5、代码优先、SQLServer2012Express在模型中实现唯一值的最佳做法是什么?我有一个places类,它有一个“url”属性,每个地方都应该是唯一的。publicclassPlace{[ScaffoldColumn(false)]publicvirtualintPlaceID{get;set;}[DisplayName("DateAdded")]publicvirtualDateTimeDateAdded{get;set;}[Required(ErrorMessage="PlaceNameisrequired")][StringLength(10

c# - MVC 6 多个获取方法

我正在尝试为每个Controller支持多个Get()方法,以及仅通过webapi访问的特别命名的方法。我已经在MVC5中完成了此操作,但似乎无法弄清楚它是如何在MVC6中完成的。有什么想法吗?谢谢。 最佳答案 不能有多个具有相同url模式的Get方法。您可以使用属性路由并为不同的url模式设置多个GET方法。[Route("api/[controller]")]publicclassIssuesController:Controller{//GET:api/Issues[HttpGet]publicIEnumerableGet()

c# - 如何更改 URL 中的 ASP.NET MVC Controller 名称?

如果我们有"example_name",我们可以使用[ActionName("")]在url中更改它所以,我想为Controller名称执行此操作。我能做到:ControllerName>example_nameController>在URL中:"/example_controller"我想在URL中像这样更改Controller名称:"/example-conroller" 最佳答案 您需要使用AttributeRouting,MVC5中引入的功能。根据您的示例,您应该按如下方式编辑您的Controller:[RoutePrefi

c# - 如何从 MVC4 中的 web.config 获取字符串值

我想获得一个logFilePath值,该值是我通过硬编码输入到appSettings中的。我试图通过达到关键值System.Configuration.ConfigurationrootWebConfig1=System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);System.Configuration.KeyValueConfigurationElementcustomSetting=rootWebConfig1.AppSettings.Settings["azureLogUrl"];st

c# - 自定义 ASP.NET MVC ActionFilterAttribute - 永远不会调用 Hook

您好,我正在尝试做一些看起来很简单的事情,并且以这种方式进行了记录,但由于某种原因,它并没有那么容易。基本上我是这样写的:publicclassCacheControllAttribute:ActionFilterAttribute{publicoverridevoidOnResultExecuting(ResultExecutingContextfilterContext){//dosomethingbase.OnResultExecuting(filterContext);}}然而,当我尝试在这样的操作结果上使用它时:[CacheControllAttribute]publicAc

c# - 在 ASP.NET MVC Core 应用程序 (RC2) 中显示项目版本

如何显示project.json中的应用程序版本?我正在使用gulp-bump来自动增加版本,但我无法显示最新版本。这是我正在尝试的:@(Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion)这不起作用,它显示“1.0.0”而不是来自project.json的实际值我也试过了,但看起来它在RC2中不再有效:@injectMicrosoft.Extensions.PlatformAbstractions.IApplicationEnvironmen

c# - 在 MVC4 中添加 Controller 不起作用

我使用的是VS2010Premium。我有一个使用SqlCe4.0和EntityFramework模型的MVC4项目。模型是:publicclassProjectBuild{publicintProjectBuildID{get;set;}publicstringname{get;set;}}publicclassProjectBuildContext:DbContext{publicDbSetbuilds{get;set;}}下面是我的连接字符串:addname="ProjectBuildContext"connectionString="DataSource=|DataDirect

c# - Mvc ViewBag - 无法将 null 转换为 'bool',因为它是不可为 null 的值类型

我想在生成某个View时在Controller中将bool设置为true,然后相应地更改View的标题。这应该非常简单,但我得到的是:CannotperformruntimebindingonanullreferenceExceptionDetails:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:Cannotperformruntimebindingonanullreference我所做的一切都在Controller中:[AllowAnonymous]publicActionResultRegister(){ViewBag

c# - mvc 5 检查用户角色

如何在mvc5中找到登录用户的角色?我用这段代码造就了用户privateboolAddUserAndRole(){IdentityResultir;varrm=newRoleManager(newRoleStore(newApplicationDbContext()));ir=rm.Create(newIdentityRole("admin"));varuser=newApplicationUser(){UserName="Admin"};varresult=UserManager.Create(user,"somepassword");UserManager.AddToRole(us

c# - T4代码生成: access types in current project

使用T4代码生成,是否可以访问当前项目中定义的类型?例如,如果我有一个接口(interface)并且我想将它的实现委托(delegate)给另一个类,即interfaceIDoSomething{publicvoiddo_something();}classDoSomethingImpl:IDoSomething{publicvoiddo_something(){//implementation...}}classSomeClass:IDoSomething{IDoSomethingm_doSomething=newDoSomethingImpl();//forwardcallstoi