asp.net-core-mvc

Convention based binding in ASP.NET 5 / MVC 6

让人想犯罪 __ 提交于 2019-12-05 06:21:05
It is possible to register dependencies manually: services.AddTransient<IEmailService, EmailService>(); services.AddTransient<ISmsService, SmsService>(); When there are too much dependencies, it becomes difficult to register all dependencies manually. What is the best way to implement a convention based binding in MVC 6 (beta 7)? P.S. In previous projects I used Ninject with ninject.extensions.conventions . But I can't find a Ninject adapter for MVC 6. No, there is no support for batch registration in the ASP.NET 5 built-in DI library. As a matter of fact, there are many features that are

A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 06:17:52
I'm trying to build a custom view location system. public class myViewLocationExpander : IViewLocationExpander { private myDBContext _context; public myViewLocationExpander (myDBContext context) { _context = context; } public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { _context.Property.//..... //Error happened here Some other codes } And in my startup file public void ConfigureServices(IServiceCollection services) { //other codes services.AddMvc(); services.Configure<RazorViewEngineOptions>(options => { options

Bad Request - Invalid Hostname when accessing localhost Web API or Web App from across LAN

与世无争的帅哥 提交于 2019-12-05 06:03:23
I have an ASP .Net Core 1.1 Web API and Web App running on my localhost from Visual Studio 2017 on Windows 10. When I run the projects, the API runs on http://localhost:50082/api/ and the web app on http://localhost:60856/ However, if others on the LAN try to access it (using my computer's IP address - http://192.168.1.101:60856/ they get a Bad Request - Invalid Hostname Error. In fact,. I get this error too of I use my IP address. I've tried about a dozen variations in my C:\Users\Documents\IISExpress\config\applicationhost.config file, such as: <bindings> <binding protocol="http"

.NET core custom and default binding combined

柔情痞子 提交于 2019-12-05 05:59:37
I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model individually from the ModelBindingContext , I would to be able to get the framework to bind the model for me, then I would carry out any custom binding: public class ApplicationViewModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof

Cannot debug in Visual Studio after changing the port number?

早过忘川 提交于 2019-12-05 05:42:02
I added the line .UseUrls("http://*:5000") to enable clients from other hosts accessing the web api. public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseUrls("http://*:5000") // Added .Build(); host.Run(); } However, using browser to access localhost:5000/api/Test got the error of HTTP/1.1 400 Bad Request ? Should the .UseUrls() be only compiled for production? HTTP/1.1 400 Bad Request Date: Mon, 08 Aug 2016 21:42:30 GMT Content-Length: 0 Server: Kestrel The

Custom cookie authentication not working after migration from ASP.NET Core 1.1 MVC to 2.0

北战南征 提交于 2019-12-05 05:39:49
I have migrated an ASP.NET Core 1.1 MVC project to ASP.NET Core 2.0 and now I note that requests to unauthorized sections of the application no longer result with a "401 Unauthorized" response but rather with a code exception leading to a response "500 internal server error". An example excerpt from the log file (John Smith is not authorized to acces the controller action he tried to access): 2018-01-02 19:58:23 [DBG] Request successfully matched the route with name '"modules"' and template '"m/{ModuleName}"'. 2018-01-02 19:58:23 [DBG] Executing action "Team.Controllers.ModulesController.Index

What is IViewLocationExpander.PopulateValues() for in Asp.Net Core MVC

社会主义新天地 提交于 2019-12-05 05:34:18
I'm using ASP.NET MVC CORE. I have implemented my own ViewLocationExpander so that I can structure my project the way I want and place my views where I like. This is accomplished by implementing a class that inherits from IViewLocationExpander and most of the work occurs in the following method: ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) Everything is working pretty sweet but the interface defines a 2nd method that I don't know how to properly implement: PopulateValues(ViewLocationExpanderContext context) I've read articles all over the internet

how to plugin custom viewengine in MVC 6 beta7?

我们两清 提交于 2019-12-05 05:30:45
in beta6 we were able to plugin a custom viewengine like this: services.AddMvc() .AddViewOptions(options => { options.ViewEngines.Clear(); options.ViewEngines.Add(typeof(MyCustomViewEngine)); }); this no longer works in beta7 and options.ViewEngines seems to have changed to an IList<IViewEngine> I don't understand how to plug one in without having to new it up and provide its dependencies options.ViewEngines.Add(new it up here?); How can I plug in my own custom viewengine in beta7? I figured it out, before calling services.AddMvc() I need to add my viewengine to DI services.TryAddSingleton

Recursion in ASP.NET Core Razor views

谁说我不能喝 提交于 2019-12-05 05:04:36
I have the following code right now to write a flat list of items with a link to a controller action: <ul> @foreach (var item in items) { <li> <a asp-controller="Home" asp-action="Demo" asp-route-itemName="@item.Name"> @item.Name </a> </li> } </ul> Now this must become recursive. Items can also contain subitems. For recursion I need some sort of function. I know I could use @functions and define the function in the .cshtml file. Not sure whether such nice inline HTML code with tag helpers would still be allowed there, it didn't seem so. Another option is HTML helpers in a .cs file, no inline

.net Core - Default controller is not loading when Route attribute is used

回眸只為那壹抹淺笑 提交于 2019-12-05 04:52:40
A new .net core web application project comes with the following route configuration: app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); If you replace this with app.UseMvc() and add appropriate Route attributes for both HomeController and its actions (Index, About, Contact, Error), it would still work. Since we're not specifying a default route, the default view (Home/Index) will not be rendered if you hit http://localhost:25137/ . Hope that understanding is correct! Now, since I need the default view to be shown when the http:/