asp.net-core-mvc

ASP.NET Core Filters - ignore for defined methods

别等时光非礼了梦想. 提交于 2019-12-05 19:23:38
We implement a log information to our database. I will use a Filters (IActionFilter) functionality for it. I wrote the following class: public class ActionFilter: Attribute, IActionFilter { DateTime start; public void OnActionExecuting(ActionExecutingContext context) { start = DateTime.Now; } public void OnActionExecuted(ActionExecutedContext context) { DateTime end = DateTime.Now; double processTime = end.Subtract(start).TotalMilliseconds; ... some log actions } } Then I have added the following code to the Startup.cs: services.AddMvc(options => { options.Filters.Add(typeof(ActionFilter)); })

How to Find All Controller and Action

蹲街弑〆低调 提交于 2019-12-05 18:56:03
问题 How to find all controllers and actions with its attribute in dotnet core? In .NET Framework I used this code: public static List<string> GetControllerNames() { List<string> controllerNames = new List<string>(); GetSubClasses<Controller>().ForEach(type => controllerNames.Add(type.Name.Replace("Controller", ""))); return controllerNames; } public static List<string> ActionNames(string controllerName) { var types = from a in AppDomain.CurrentDomain.GetAssemblies() from t in a.GetTypes() where

How to handle errors differently for (or distinguish between) API calls and MVC (views) calls in ASP.NET Core

亡梦爱人 提交于 2019-12-05 18:43:30
问题 In my applications based on ordinary MVC and WebApi I had two different error handling routes. If an error occurred during WebApi call, I would intercept it (using standard web api options) and return json message with corresponding HTTP Status Code so that client app can handle it. If the error happened in MVC, then I would use some standard handlers that would redirect user to some default error screen possibly based on status code. Now in ASP.NET Core both are joined in the same framework,

The property is of an interface type ('IFormFile') MVC Core

北战南征 提交于 2019-12-05 18:41:46
I'm trying to make a form that i could save a file(image) , but it shows me an error: InvalidOperationException: The property 'Product.Image' is of an interface type ('IFormFile'). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type, otherwise ignore the property from the model. Apply I dont know how to fix it , here's the code: Product.cs public class Product { public Product() { OrderDetails = new HashSet<OrderDetails>(); } public int Id { get; set; } public string Name { get; set; } public string Description { get; set;

Back button issue in MVC 6 after login and logout

☆樱花仙子☆ 提交于 2019-12-05 18:30:02
I am trying to restrict user to click back button after Login/Logout into the application. If the user is logged in into the application, then after clicking on back button Login view should not be displayed and if user is logged out from the application then clicking on back button LogOff view should not be displayed. I have already used caching technique which is given in number of links, but seems its not working. This CacheAfterLogout link and done exactly as it is, still problem is not solved. I am using asp.net identity framework. Could someone help on this ? To disable caching you can

Using WebGrid in ASP.NET Core

浪子不回头ぞ 提交于 2019-12-05 17:09:17
I am using System.Web.Helpers.WebGrid control in ASP.NET Core application, I am getting the below error while rendering the grid An unhandled exception occurred while processing the request. ArgumentNullException: Value cannot be null. Parameter name: httpContext System.Web.HttpContextWrapper..ctor(HttpContext httpContext) at line System.Web.Helpers.WebGrid objWebGrid = new System.Web.Helpers.WebGrid(Model.MeetingsObj); Model: public class Meeting { public int MeetingId { get; set; } public string MeetingName { get; set; } public string Format { get; set; } public string Location { get; set; }

PagedList.Core.Mvc PagedListPager Html extension in .Net Core is not there

自古美人都是妖i 提交于 2019-12-05 16:38:38
It seems like the PagedList.Core does not contain the extension method for Html helper, so I cannot use the code below: @Html.PagedListPager(Model, page => Url.Action("Index", new { page }), PagedListRenderOptions.MinimalWithItemCountText) I was able to successfully implement the paging in previous version of MVC, but it does not work in ASP.Net Core. Below I have attached IL Dasm reference. Am I missing something, or is there any other way to implement that? PagedList.Mvc: PagedList.Core.Mvc: I should follow the Instructions of the new version, it is a little different from previous version.

ASP .NET Core: CORS headers only for certain static file types

故事扮演 提交于 2019-12-05 16:08:49
I have an ASP .NET Core self-hosted project. I am serving up content from a static folder (no problem). It serves up images cross-site without issue (CORS header shows up). However, for some file types, such as JSON, they CORS headers don't show up, and the client site can't see the content. If I rename the file to an unknown type (such as JSONX), it gets served with CORS headers, no problem. How can I get this thing to serve everything with a CORS header? I have the following CORS policy set up in my Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddCors

How to Per-Request caching in ASP.net core

做~自己de王妃 提交于 2019-12-05 15:26:02
问题 My old code looks like this: public static class DbHelper { // One conection per request public static Database CurrentDb() { if (HttpContext.Current.Items["CurrentDb"] == null) { var retval = new DatabaseWithMVCMiniProfiler("MainConnectionString"); HttpContext.Current.Items["CurrentDb"] = retval; return retval; } return (Database)HttpContext.Current.Items["CurrentDb"]; } } Since we don't have HttpContext anymore easily accesible in core, how can I achieve the same thing? I need to access

ASP.NET MVC 6 handling errors based on HTTP status code

a 夏天 提交于 2019-12-05 15:23:14
问题 I want to display different error messages for each status code e.g: 400 Bad Request 403 Forbidden 500 Internal Server Error 404 Not Found 401 Unauthorized How can I achieve this in the new ASP.NET MVC 6 applications? Can I do this using the built in UseErrorHandler method? application.UseErrorHandler("/error"); Also, I noticed that even with the above handler, entering a non-existent URL e.g. /this-page-does-not-exist, causes an ugly 404 Not Found error page from IIS. How can this also be