Tag: asp.net core mvc

在dotnetcore中重新加载运行时的路由

我有一个自定义路由,从无SQL数据库(MongoDB)读取URL,并在应用程序启动时将它们添加到路由管道,这是“非常标准” 像这样的东西(在我的startup.cs文件中): app.UseMvc(routes => { routes.MapRoute( “default”, “{controller=Home}/{action=Index}/{id?}”); routes.Routes.Add( new LandingPageRouter(routes, webrequest, memoryCache, Configuration)); // this custom routes adds URLs from database } 问题是,如果我在应用程序启动后向数据库添加另一个路由,我基本上得到404,因为路由系统不知道这个新路由,我认为我需要的是在运行时添加缺少的路由或(不方便)从另一个Web应用程序(已在框架4.5上开发,显然它在不同的池上运行)重新启动应用程序池 还有其他想法吗? 谢谢。

ASP.NET Core身份validationcookie只收到一次

我正在使用ASP.NET Core开发应用程序,我正在使用自定义Cookie身份validation。 我的CookieAuthenticationOptions是: app.UseCookieAuthentication(new CookieAuthenticationOptions() { AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme, LoginPath = new PathString(“/login”), AccessDeniedPath = new PathString(“/unauthorized/”), AutomaticAuthenticate = true, AutomaticChallenge = true }); cookie创建得很好,我可以在运行应用程序的整个过程中在浏览器设置中看到它。 这是我的HomeController类: public HomeController(IHostingEnvironment env, IAntiforgery antiforgery, IOptions appSettings, TerminalDbContext terminalContext, ILoggerFactory loggerFactory, IHttpContextAccessor _httpContextAccessor) { _env = env; _antiforgery = antiforgery; _appSettings = appSettings; _terminalContext = terminalContext; _logger = loggerFactory.CreateLogger(); […]

ASP.NET Core中必需的查询字符串参数

使用ASP.NET Core 1.1和VS2015(sdk 1.0.0-preview2-003131),我有以下控制器: public class QueryParameters { public int A { get; set; } public int B { get; set; } } [Route(“api/[controller]”)] public class ValuesController : Controller { // GET api/values [HttpGet] public IEnumerable Get([FromQuery]QueryParameters parameters) { return new [] { parameters.A.ToString(), parameters.B.ToString() }; } } 如您所见,我有两个查询参数。 我想要的是要求其中一个(例如: A )。 也就是说,我想使用一个属性(如果可能)来说明这个属性是必需的。 然后,我希望ASP.NET在调用我的控制器之前进行此validation。 我本来希望使用Newtonsoft […]

如何禁用net core 2.1中的预编译视图进行调试?

昨天我更新到net core 2.1。 现在,如果我正在调试,那么预编译的视图,在启动过程中需要很长时间…是否有可能回退到以前的行为,如果需要的话,及时编译视图? 我的csproj中没有与预编译有关的参考。 它是来自元包的东西吗? <!—->

MVC 6 IUrlHelperdependency injection

我想通过dependency injection使用IUrlHelper,以便能够使用其function为不同的rest端点生成uris。 我似乎无法弄清楚如何从头开始创建UrlHelper,因为它在MVC 6中发生了变化,而MVC并没有自动在IoC控制器中提供该服务。 设置是我的控制器接受api模型转换器类的内部模型,并使用IUrlHelper(通过Depenedency Injection)。 如果有一个更好的替代IUrlHelper / UrlHelper,我可以用来为我的WebApi动作/控制器生成Uris,我愿意接受建议。

Asp.Net核心unit testing异步控制器

我有这个测试: [Fact] public async void Can_Paginate() { //fake data var product1 = new Product { ProductId = 1, ProductName = “P1” }; var product2 = new Product { ProductId = 2, ProductName = “Product 2” }; var product3 = new Product { ProductId = 3, ProductName = “Product 3” }; var product4 = new Product […]

如何对数据库中的用户使用Windows身份validation

我的主要目标是使用Windows身份validation来查询我的自定义Users表以通过Web应用程序使用。 我不确定有这样做的传统方法。 我在SQL数据库中有一个预定义的Users表和Roles表。 如何使用User.Identity.Name查询此Users表并将所有表数据以及角色映射到ApplicationUser类,以后可以在Intranet Web应用程序中进一步使用? 通过阅读大量文章,我无法找到与我所追求的任何密切相关的东西。 我假设这将在ConfigureServices下的Startup类中完成,但我也不确定。 我需要在第一次导航到网站时查找用户。

在mvc中使用IViewLocationExpander

我想从自定义位置渲染视图,所以为此我在类中实现了IViewLocationExpander接口。 我在startup file注册了相同的类,如下所示。 Startup.cs文件 public void ConfigureServices(IServiceCollection services) { ….. //Render view from custom location. services.Configure(options => { options.ViewLocationExpanders.Add(new CustomViewLocationExpander()); }); …. } CustomViewLocationExpander类 public class CustomViewLocationExpander : IViewLocationExpander { public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable viewLocations) { var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService(); string folderName = session.GetSession(“ApplicationType”); viewLocations = viewLocations.Select(f => f.Replace(“/Views/”, “/” + folderName + “/”)); […]

ASP.Net核心MVC6未经授权重定向到登录

我正在使用ASP.Net核心MVC 6,我试图让用户重定向到登录页面,如果他们没有经过身份validation。 我似乎无法让它工作,目前用户只是得到一个空白页面。 下面是我在Startup.cs中的ConfigureServices方法 public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”)) ); services.AddIdentity(options => { // configure identity options options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireUppercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequiredLength = 7; options.Cookies.ApplicationCookie.AutomaticAuthenticate = true; options.Cookies.ApplicationCookie.AutomaticChallenge = true; options.Cookies.ApplicationCookie.LoginPath = “/Account/Login”; // User settings options.User.RequireUniqueEmail = true; }) […]

在ASP.NET 5中请求BinaryRead(MVC6)

我有这个代码在ASP.NET MVC 5中工作,但我不能使它在ASP.NET MVC 6(ASP.NET 5)中工作 有人能帮我吗? public EmptyResult PayPalPaymentNotification(PayPalCheckoutInfo payPalCheckoutInfo) { PayPalListenerModel model = new PayPalListenerModel(); model._PayPalCheckoutInfo = payPalCheckoutInfo; byte[] parameters = Request.BinaryRead(Request.ContentLength); if (parameters != null) { model.GetStatus(parameters); } return new EmptyResult(); } 错误在于: byte[] parameters = Request.BinaryRead(Request.ContentLength); HttpRequest不包含BinaryRead的定义,也没有扩展方法BinaryRead接受类型为HttpRequest的第一个参数(你是否缺少using指令或程序集引用?)。 我已经测试了这样的事情,但没有工作: HttpContext.Request.BinaryRead 谢谢。 编辑:类似问题 – > 二进制读取错误