Tag: asp.net core

渴望加载多对多 – EF Core

您好我有很多关系设置如下。 public class order { public int id { get; set; } public virtual ICollection Products { get; set; } } public class product { public int id { get; set; } public virtual ICollection Orders { get; set; } } public class OrderProducts { public int OrderId { get; set; } public virtual Order […]

AmbiguousActionException:匹配多个动作。 以下操作匹配路由数据并满足所有约束

我正在使用ASP.NET Core MVC创建一个网站。 当我点击某个动作时,我收到此错误: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: Web.Controllers.ChangeEventsController.Create (Web) Web.Controllers.ProductsController.CreateChangeEvent (Web) 这就是我在我的ProductsController的index.cshtmlm中定义我的动作的方法: Create Change Event 这是我的路线: app.UseMvc(routes => { routes.MapRoute( name: “default”, template: “{controller=Home}/{action=Index}/{id?}”); }); 以下是我定义操作的方式: // ChangeEventsController [HttpGet(“{id}”)] public IActionResult Create(Guid id) // ProductsController [HttpGet(“{id}”)] public IActionResult CreateChangeEvent(Guid id) 我做错了什么? 更新 感谢@MegaTron的回复,但是我想知道为什么我不能为不同的控制器提供相同的操作路径。 […]

如何在.NET Core中对Startup.cs进行unit testing

人们如何在.NET Core 2应用程序中对其Startup.cs类进行unit testing? 所有function似乎都是由静态扩展方法提供的,这些方法不可模拟? 例如,如果您使用此ConfigureServices方法: public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”))); services.AddMvc(); } 如何编写测试以确保调用AddDbContext(…)和AddMvc(),通过Extensions方法实现所有这些function的选择似乎使其不可测试?

从类型中获取DbSet

我正在尝试为MVC 6应用程序创建通用表查看器/编辑器。 我目前正在使用 Context.GetEntityTypes(); 给我一个表格列表。 现在我需要获取特定类型的数据。 我目前的实施是: // On my context public IQueryable GetDbSetByType(string fullname) { Type targetType = Type.GetType(fullname); var model = GetType() .GetRuntimeProperties() .Where(o => o.PropertyType.IsGenericType && o.PropertyType.GetGenericTypeDefinition() == typeof(DbSet) && o.PropertyType.GenericTypeArguments.Contains(targetType)) .FirstOrDefault(); if (null != model) { return (IQueryable)model.GetValue(this); } return null; } 在我的控制器中使用此代码 [HttpGet(“{requestedContext}/{requestedTable}/data”)] public IActionResult GetTableData(string requestedContext, string requestedTable) { […]

如何在asp.net vnext中注册身份validation类型名称

因此,我正在更新MongoDB的开源asp.net身份提供程序,以便与Asp.Net Identity 3.0(又名vnext)一起使用。 到目前为止,我已经能够注册提供程序并创建用户,但是当使用SignInManager时,如果提供了正确的UserName / Pass,我会收到错误 InvalidOperationException:未接受以下身份validation类型:Microsoft.AspNet.Identity.Application 我已经将错误跟踪到这里https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.PipelineCore/DefaultHttpResponse.cs 但我似乎无法看到SignInContext.Accepted名称被添加到SignInContext的位置。 我正在使用VS14 CTP2中使用的所有vnext库的Alpha-2版本 下面是我的Startup.cs public void Configure(IBuilder app) { try { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 var configuration = new Configuration(); configuration.AddJsonFile(“config.json”); configuration.AddEnvironmentVariables(); // app.UseLogRequests(“try”); app.UseErrorPage(ErrorPageOptions.ShowAll); app.UseServices(services => { services.AddIdentity() .AddMongoDB(configuration.Get(“Data:MongoIdentity:ConnectionString”), configuration.Get(“Data:MongoIdentity:DBName”)) .AddHttpSignIn(); // Add MVC services to the services […]

在ASP.NET Core中的Program.Main中访问环境名称

使用ASP.NET Mvc Core我需要将我的开发环境设置为使用https,因此我将以下内容添加到Program.cs中的Main方法: var host = new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup() .UseKestrel(cfg => cfg.UseHttps(“ssl-dev.pfx”, “Password”)) .UseUrls(“https://localhost:5000”) .UseApplicationInsights() .Build(); host.Run(); 如何在此处访问托管环境,以便我可以有条件地设置协议/端口号/证书? 理想情况下,我会使用CLI来操纵我的托管环境,如下所示: dotnet run –server.urls https://localhost:5000 –cert ssl-dev.pfx password 但似乎没有办法从命令行使用证书。

循环遍历List

我试图获取每个项目的值与数组列表中的索引我用这个代码做了它,但没有在循环完成后显示值。 foreach (var item in (List)TempData[“verification_errors”]) { @item[0].ToString() + @item[2].ToString() } 这是List List Data_verification_errors = new List(); string[] Cust_Surname = new string[] { “CustomerSurname”, reservation_details.CustomerSurname, “Customer name must have at least 2 characters” }; Data_verification_errors.Add(Cust_Surname);

无法使用Asp.NET Core中的Entity Framework添加带有视图的Scaffold> MVC Controller

当我尝试使用内置生成器添加Controller时,我收到以下错误消息: 其中“Brand”是模型类,“ApplicationDbContext”是我的数据上下文类。 我从来没有像这样经历过这样的错误。 它不会一直发生(2次成功与30-40次尝试而不更改单行代码)。 我能够在几分钟之前为同一个类创建Controller,但是当你尝试添加新内容并尝试超过10次而没有结果时,它真的很烦人。 当然,我可以编写自己的控制器和视图,但我真的很喜欢使用附带的工具。 我已经使用“添加或删除”中的“修改”选项修复了VS. 我正在使用最新的Microsoft Visual Studio社区(MVSC2015 V14.0.25425.01 Update3)。 编辑: Brand.cs: namespace AppBrander.Models { public class Brand { public int ID { get; set; } public string Email { get; set; } public string BrandName { get; set; } public string CompanyName { get; set; } public string Description { get; set; […]

如何将服务配置传递给Xunit项目测试控制器?

我目前有以下设置。 启动类: Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection(“AzureStorageConfig”)); services.AddTransient(); } //other config settings … } 类: AzureStorageConfig //store the azure account details etc… public class AzureStorageConfig { public string AzureURL { get; set; } public string […]

如何在ASP.NET Core RC2中制作websocket服务器?

我使用“Microsoft.AspNetCore.WebSockets.Server”:“0.1.0-rc2-final”来制作websocket服务器,但不起作用。 (在rc1中,它是1.0.0-rc1-final,它工作正常…) 这是我的代码: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection(“Logging”)); loggerFactory.AddDebug(); app.UseWebSockets(); app.Use(async (http, next) => { if (http.WebSockets.IsWebSocketRequest) { IHandler handler = WebsocketManager.getHandler(http.Request.Path); if (handler == null) { await next(); } else { var webSocket = await http.WebSockets.AcceptWebSocketAsync(); if (webSocket != null && webSocket.State == WebSocketState.Open) { // TODO: Handle the […]