天道酬勤,学无止境

asp.net-core

What is the alternate of [AllowHtml] in ASP.Net Core 2.0 [duplicate]

This question already has an answer here: Generate web page from Database using AllowHtml in Asp.Net Core 2.0 (1 answer) Closed 3 years ago. I want to integrate CKEditor in my MVC Core 2.0 Application, in previous version I used it by adding [AllowHTML] data annotation to my string property. But in ASP.Net Core I could not find the right way to insert HTML into string input. My code in in ASP.Net MVC 5 [AllowHtml] [DataType(DataType.MultilineText)] public string Profile { get; set; } but in ASP.Net Core 2.0 [AllowHtml] is not working. I searched in google but could not find right solution

2021-06-15 18:02:35    分类:问答    asp.net-mvc   asp.net-core   asp.net-core-mvc   asp.net-core-2.0

Session.SetString method throws exception “IFeatureCollection has been disposed. Object name: 'Collection'. ” in ASP.NET Core 3.1

I have a project written in ASP.NET Core 3.1. I need to set data to Session in Singleton service: _session.SetString("some key", "some value"); I injected the session object from DI: public OperatorService(ILogger<OperatorService> logger, ISession session, IOptions<AppSettings> options) { this._session = session; this._logger = logger; this._appSettings = options.Value; } I calls the my method as below: public void ChangeOperatorStatus(StatusChangeRequest request) { try { _session.SetString(request.Key, request.Value); } catch (Exception ex) { _logger.LogInformation($"Exception while changing

2021-06-15 16:06:04    分类:问答    c#   .net   asp.net-core   asp.net-core-3.1

How to Authorize Swagger to use MS Graph API

We are building a Web API wrapper for MS Graph API. I want to use Swagger to test my APIs. But I can't get the configuration right. I keep getting Bad Request and no other clue. I can't install Fiddler or other tools on this corporate laptop to help me investigate. Here is the Error And here is the code to configure Swagger: app.UseSwaggerUi3WithApiExplorer(settings => { settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase; settings.PostProcess = document => { document.Info.Title = "App title"; document.Info.Description = "App description"; }; settings

2021-06-15 15:34:49    分类:问答    c#   asp.net-core   swagger-ui   azure-ad-graph-api   nswag

Disable/Remove '?ReturnUrl=' from Url's in netcore 2

I am trying to find a way to prevent my aspnetcore application to add "?ReturnUrl=" to the URL. Does anyone know how to do it, using some kind of middleware. I tried doing it like below but it did not have any effect: public class RequestHandlerMiddleware { private readonly RequestDelegate _next; public RequestHandlerMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { if(context.Request.QueryString.HasValue && context.Request.QueryString.Value.Contains("?ReturnUrl=")) { context.Request.QueryString = new QueryString(string.Empty); } await _next

2021-06-15 15:32:08    分类:问答    c#   asp.net-core   asp.net-core-2.0

Hosting Web API in .Net Core Worker Service - cannot reference IWebHostEnvironment

I'm creating a .NET Core Worker Service, and want to expose ASP.Net Core Web APIs for the service. I'm using .NET Core 3.0. Initially, my plan was to replace IHostBuilder with IWebHostBuilder and add a Startup class just like a regular Asp.Net Core web app (this is probably an oversimplification, though). My plan was to simply try to replace public static IHostBuilder CreateHostBuilder(string[] args) { return Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); }); } with public static IWebHostBuilder CreateHostBuilder(string[]

2021-06-15 15:07:39    分类:问答    asp.net-core   .net-core

Trouble trusting local HTTPs certificate in ASP.NET Core

I am following an online tutorial to create an ASP.NET Core Web application. I followed everything but had trouble with trusting the local HTTPS certificate. When I first run the application, I got an error saying: "the access control list (acl) structure is invalid" I tried googling this error but couldn't find anything related to my ASP.NET application, after cleaning the solution I didn't get this error any longer. But I was unable to run my application in browser using HTTPS as it always says: "This site is not secure" I tried to trust certificate using CMD using this command: dotnet dev

2021-06-15 14:51:28    分类:问答    c#   asp.net-core   ssl-certificate

User.GetUserId() fails inside controller's constructor

I am getting the following error: Value cannot be null. Parameter name: principal How can I access Identity (userId) inside the controller's constructor? I can only get it working by wrapping the failed call in a function (both highlighted below). Is there anything I need to inject? public class BaseController : Controller { protected readonly MylDbContext dbContext; protected readonly string userId; public BaseController(MylDbContext dbContext) { this.dbContext = dbContext; userId = User.GetUserId(); // fails here } public string GetUserId() { return User.GetUserId(); // works here } }

2021-06-15 14:24:04    分类:问答    asp.net   asp.net-core   asp.net-core-mvc

asp.net core development model

Error. An error occurred while processing your request. Development Mode Swapping to Development environment will display more detailed information about the error that occurred. Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application. how do i use the development model after publish iis

2021-06-15 13:58:28    分类:问答    asp.net-mvc   asp.net-core

Conventions on having both an API and MVC project in .NET Core solution

I have an ASP.NET Core (.NET Core 2.2) app structured with the following projects: API: this is meant to represent a WebAPI (with controllers inheriting ControllerBase) Services: This contains services which the API controllers utilize to access the database, etc Database: This contains the usual DB repositories, which the services layer utilize to access the database Now, I want to add a UI that talks to the API (the MVC part pre-.NET-core). How is that accomplished with .NET Core, where MVC and WebAPI are one of the same thing? Should MVC controllers/models/views be part of the API? Should

2021-06-15 13:58:22    分类:问答    c#   asp.net   asp.net-mvc   asp.net-web-api   asp.net-core

JSON returns properties in PascalCase instead of camelCase

Using Microsoft.AspNetCOre.OData 7.0.1, if I have a list of Models that are NOT in the database, the JSON result always comes back as PascalCase instead of camelCase. How can I get my list to be camelCase? Relative example below: My model that is NOT in the database. public class Widget { public string Id { get; set; } public string Name { get; set; } } My controller [Route("api/[controller]")] public class WidgetController : ODataController { [EnableQuery()] public IActionResult GetWidgets() { // Create list of ten Widgets var widgetsList = new List<Widget>(); for(var i = 0; i < 10; i++) {

2021-06-15 13:54:11    分类:问答    c#   asp.net-core   odata