asp.net-core-mvc

Returning exceptions as JSON messages

可紊 提交于 2019-12-04 05:03:30
I am developing an API with ASP.NET Core and I am struggling with the exception handling. When any exception occurs, or in any controller where I want to return custom errors with different status codes, I want to return JSON-formatted exception reports. I do not need an HTML in the error responses. I'm not sure if I should use middleware for this, or something else. How should I return JSON exceptions in an ASP.NET Core API? An exception filter (either as an attribute, or a global filter) is what you are looking for. From the docs : Exception filters handle unhandled exceptions, including

How to Find All Controller and Action

人走茶凉 提交于 2019-12-04 04:40:39
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 typeof(IController).IsAssignableFrom(t) && string.Equals(controllerName + "Controller", t.Name,

How to logout all users in ASP.NET Core cookie authentication?

…衆ロ難τιáo~ 提交于 2019-12-04 04:40:13
I'm using ASP.NET Core MVC with CookieAuthentication. Is there a way I can sign all users out at once? I tried resetting IIS - didn't work. I tried deleting all the users' sessions (I'm using a database for session storage) - didn't work. Any ideas? With CookieAuthentication, the cookie is simply an encrypted string containing the user's name, roles, and auxilliary data. In short, it identifies the user , not the session. Killing sessions does not invalidate the cookie. That being said, you can stuff a session identifier or other token in the cookie's auxiliary data, and then validate that

How do I get the details of an Error 500 for an Azure Web App?

让人想犯罪 __ 提交于 2019-12-04 04:35:40
I have an MVC 6 site that has been deployed as an Azure Web App. I have enabled all tracing / diagnostic options in Visual Studio using Server Explorer. I am trying to do remote debugging, but I am only seeing an Error 500 on my browser, and not finding any information (or triggering an error breakpoint) on where exactly the error occurred. I can't find any error trace on the file logs that I get from Azure. How should I troubleshoot Error 500's in an Azure Web App? Adding "CustomErrors": {"Mode": "Off"} to config.json didn't work. For MVC 6 you need to add a web.config file to the wwwroot

Debugging not hit breakpoints in .NET CORE MVC 6 application

£可爱£侵袭症+ 提交于 2019-12-04 04:30:14
I am working on .NET CORE 1.0 MVC 6 application and I stuck with the debugging point as it stopping hitting yesterday. with number of try I delete project and start again. First time it load symbols even due I have uncheck in Tool --> Debugging --> symbols, however it hit breakpoints. Now it only hitting C# class 'Startup.cs' if I choose 'Enable Just My Code' but in controller. I have Debug option from dropdown, not really sure why. Need help here. I change as Select Debug->Options->Debugging->General Tick Enable .NET Framework source stepping. but still no success Module Trying to Hit in

What do I do when ASP.NET 5 (vNext) can't redirect bindings?

亡梦爱人 提交于 2019-12-04 04:00:51
问题 I am just getting my feet wet with MVC 6. I installed VS 2015 and with the default ASP.NET 5 preview MVC Web Application template everything runs fine under local IIS. I then tried to switch out the Default DI container with StructureMap following these instructions exactly (note it is a very recent article). The only thing is I had to figure out the namespaces to import myself (since the author neglected to include them) and this is what I included. I put the StructureMapRegistration class

HtmlHelpers in MVC 6

青春壹個敷衍的年華 提交于 2019-12-04 03:35:49
I'm trying to port this code over to mvc 6, any help is appreciated, the code compiles but the method is not available in my views on @Html.IsActive . using Microsoft.AspNet.Mvc.Rendering; namespace Blah.Web.Helpers { public static class HtmlHelpers { public static string IsActive(this HtmlHelper htmlHelper, string controller, string action) { var routeData = htmlHelper.ViewContext.RouteData; var routeAction = routeData.Values["action"].ToString(); var routeController = routeData.Values["controller"].ToString(); var returnActive = (controller == routeController && action == routeAction);

Asp.Net core 2.0 MVC anchor tag helper not working

删除回忆录丶 提交于 2019-12-04 03:35:07
问题 I am trying to create a demo page, and can't solve the next problem, and I tried everything what I found on the web. I have an anchor tag with tag helper: <a class="menu-link" asp-area="" asp-controller="Telefon" asp-action="Index">Telefonok</a> I also added a _ViewImports.cshtml and in it I added as I saw in the net @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers" , but I still got an error: The name 'addTagHelper' does not exist in the current context What did I wrong?! 回答1: There are two

Running MVC 6 ASP.NET 5 localization example for dnx rc2

喜欢而已 提交于 2019-12-04 03:31:25
问题 I am trying to run the AspNet5Localization example project from here https://github.com/damienbod/AspNet5Localization/tree/rc2 However, when I open the solution a warning box appears: DNX SDC version dnx-clr-win-x86.1.0.0-rc2-16444 is required by your solution but is not installed on this machine. Do you want to install it now? If you select No, 'dnx-clr-win-x86.1.0.0-rc1-update1' will be used as the solution DNX SDK version for this session. I choose Yes. Then another info box appears: DNX

How to get the database context in a controller

情到浓时终转凉″ 提交于 2019-12-04 03:26:05
I am trying all day to figure out to get the ApplicationDbContext in the ManageController.cs of a default MVC 6 project. I went online and Googled a lot but no one seems to have the same problem as I have with it. It is probably simple but I can't figure it out. Anyone has an idea? Here is what I tried: IServiceProvider service = new IServiceProvider(); var _context = service.GetService<ApplicationDbContext>(); Use constructor injection: public class ManageController { private readonly ApplicationDbContext _context; public ManageController(ApplicationDbContext context) { _context = context; }