ASP.NET Core v2 (2015) MVC : How to get raw JSON bound to a string without a type?

The cleanest option I’ve found is adding your own simple InputFormatter: public class RawJsonBodyInputFormatter : InputFormatter { public RawJsonBodyInputFormatter() { this.SupportedMediaTypes.Add(“application/json”); } public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { var request = context.HttpContext.Request; using (var reader = new StreamReader(request.Body)) { var content = await reader.ReadToEndAsync(); return await InputFormatterResult.SuccessAsync(content); } } protected override bool CanReadType(Type type) … Read more

How to clear browser cache on browser back button click in MVC4?

The problem with your approach is that you are setting it where it is already too late for MVC to apply it. The following three lines of your code should be put in the method that shows the view (consequently the page) that you do not want to show. Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); Response.Cache.SetNoStore(); If you want … Read more

Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5 [duplicate]

To resolve this I had to install the Facebook SDK for .NET from nuget and query the email address separately. In the ExternalLoginCallback method, I added a conditional to populate the email address from the Facebook Graph API; var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return RedirectToAction(“Login”); } // added the following … Read more

Progress bar for long running server calls in ASP.Net MVC [closed]

The right and easiest way to do this is with SignalR. Please download Microsoft SignalR in Create a hub class in separate folder in project path called hubs, add two class files into the hubs folder Startup.cs using Owin; using Microsoft.Owin; [assembly: OwinStartup(typeof(SignalRChat.Startup))] namespace SignalRChat { public class Startup { public void Configuration(IAppBuilder app) … Read more

Rendering Partial Views using ajax

If you want to load the page and then load the partial view via ajax you could create an ActionMethod that does something like: public ActionResult Create() { var model = new MyModel(); if (Request.IsAjaxRequest()) { return PartialView( “_Partial”, model.PartialModel ); } else { return View( model ); } } and then in your page … Read more

Why do we use ViewModels?

For smaller projects, you’re right. I hear your argument and sympathise – however there are good reasons for this, drudged and repetitive work, especially in larger and more complicated applications: It’s essential to perform all processing within the Controller’s action. However in the example you’ve given, the Repository.Get method might return a lazily-evaluated IQueryable object, … Read more

ASP.NET MVC – HTTP Authentication Prompt

Well, to require basic authentication you need to return 401 status code. But doing that will cause the current authentication module to execute its default unauthorized handler (for forms authentication, this means redirecting to login page). I wrote an ActionFilterAttribte to see if I can get the behaviour you want when there’s no authentication module … Read more