asp.net-core-mvc

MVC CORE 2.0.0 run c# code on every page

孤者浪人 提交于 2019-12-01 12:03:30
问题 I need to run some c# code each time any page based on _layout.cshtml is viewed. I don't want to put something in every controller.cs file, just something central like you used to have in ASP.NET's MasterPage.cs Can't get this Run a method in each request in MVC, C#? or this @Html.Action in Asp.Net Core to run, not sure if it's because they're not CORE 2.0.0, I just get a lot of compilation errors. All I want to do is be able to run some code like this public class myClass { public static

How to read xml document in MVC core (1.0.0)?

99封情书 提交于 2019-12-01 11:09:44
I'm new to web programming and decided to switch from .net 4.5 to .net core. My project has a static xml document in the following location: wwwroot/Countries/en-GB.xml How would one go about reading the xml file at the specified path? Eventually I will convert the data to a SelectList. In .net 4.5 I used DataSet's and HttpConext...MapPath to read the xml document which no longer works in core mvc. Any advice is greatly welcome. First of all don't put your data source into wwwroot folder, because it is served publicly. Take a look at official docs : The web root of your app is the directory in

How can add controller in ASP.NET MVC6 beta5 project?

耗尽温柔 提交于 2019-12-01 09:39:35
i'm install Visual Studio 2015 final release and create mvc6 beta5 project (by default template). Trying add controller as in mvc 5.2 (also in vs2015 final release): but, in mvc6 beta5 project this menu missing: 1. How solve the problem? UPD 2. Is it possible add mvc6 controller with generate views? Prior to MVC 6.0 the notion of an MVC Controller and a Web API Controller were somewhat different. To the untrained eye, they looked similar but they did behave differently and were using different classes. Hence why when you clicked the Controller… option, you had the ability to choose between an

How to discern between model binding errors and model validation errors?

血红的双手。 提交于 2019-12-01 09:31:45
I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is syntactically correct but contains unacceptable values). As an example, given an action like [HttpPut("{id}")] public async Task<IActionResult> UpdateAsync( [FromRoute] int id, [FromBody] ThingModel model) I'd like to return a 400 status code when the id parameter in the route contains a non-digit character or when no body has been specified in the

How can add controller in ASP.NET MVC6 beta5 project?

白昼怎懂夜的黑 提交于 2019-12-01 09:30:40
问题 i'm install Visual Studio 2015 final release and create mvc6 beta5 project (by default template). Trying add controller as in mvc 5.2 (also in vs2015 final release): but, in mvc6 beta5 project this menu missing: 1. How solve the problem? UPD 2. Is it possible add mvc6 controller with generate views? 回答1: Prior to MVC 6.0 the notion of an MVC Controller and a Web API Controller were somewhat different. To the untrained eye, they looked similar but they did behave differently and were using

Working with IViewLocationExpander in mvc

余生长醉 提交于 2019-12-01 09:28:50
I want to render the view from custom location, so for that I have implemented IViewLocationExpander interface in a class. I have registered the same class in startup file as follows. Startup.cs file public void ConfigureServices(IServiceCollection services) { ..... //Render view from custom location. services.Configure<RazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(new CustomViewLocationExpander()); }); .... } CustomViewLocationExpander Class public class CustomViewLocationExpander : IViewLocationExpander { public IEnumerable<string> ExpandViewLocations

ASP.Net core MVC6 Redirect to Login when not authorised

十年热恋 提交于 2019-12-01 09:11:16
I am using ASP.Net core MVC 6, I am trying to get the user redirected to the login page if they are not authenticated. I cant seem to get it to work, currently the user just gets a blank page. Below is my ConfigureServices method in Startup.cs public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")) ); services.AddIdentity<ApplicationUser, IdentityRole>(options => { // configure identity options options.Password.RequireDigit = true;

Create RSS feed in asp.net core 1.0

…衆ロ難τιáo~ 提交于 2019-12-01 09:09:22
I am working in Asp.net Core 1.0 MVC 6 I am trying to write a component to provide RSS feeds from my websites. I found this post that suggests that System.ServiceModel.Syndication has yet to be ported to ASP.NET CORE. I am unable to target the full .NET framework. The suggestion is to write as an xml parser. I am however struggling to get my head around everything that might be required. I have built the functionality to get my data into XML but now need to better understand how to allow this to be called from an IActionResult (or indeed how to generate a link that may be placed on my page). I

What is the correct way to create custom model binders in MVC6?

寵の児 提交于 2019-12-01 09:02:45
问题 I'm trying to follow the steps in this article using a vNext project and mvc 6. I've been reading through the code here but still a little unsure how to implement this. Does anyone have a working example they could share or point me in the right direction? I'm particularly wondering how to register the custom binder, and what classes I would inherit from since DefaultModelBinder is not available. 回答1: Sample model binder: https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Core

Oauth2 login to Google api with ASP .net core MVC

喜夏-厌秋 提交于 2019-12-01 08:34:21
I have been following Web applications (ASP.NET MVC) Attempting to connect to one of the Google APIs. using System; using System.Web.Mvc; using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; using Google.Apis.Auth.OAuth2.Mvc; using Google.Apis.Drive.v2; using Google.Apis.Util.Store; namespace Google.Apis.Sample.MVC4 { public class AppFlowMetadata : FlowMetadata { private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = "PUT_CLIENT_ID_HERE", ClientSecret =