How to get JSON object from Razor Model object in javascript

You could use the following: var json = @Html.Raw(Json.Encode(@Model.CollegeInformationlist)); This would output the following (without seeing your model I’ve only included one field): <script> var json = [{“State”:”a state”}]; </script> Working Fiddle AspNetCore AspNetCore uses Json.Serialize intead of Json.Encode var json = @Html.Raw(Json.Serialize(@Model.CollegeInformationlist)); MVC 5/6 You can use Newtonsoft for this: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model, Newtonsoft.Json.Formatting.Indented)) This gives … Read more

How to set Default Controller in asp.net MVC 4 & MVC 5

the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}”, // URL with parameters* new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } ); as the default landing page. You can change that to be any route you wish. … Read more

EntityType ‘IdentityUserLogin’ has no key defined. Define the key for this EntityType

In my case I had inherited from the IdentityDbContext correctly (with my own custom types and key defined) but had inadvertantly removed the call to the base class’s OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // I had removed this /// Rest of on model creating here. } Which then fixed up my missing … Read more

What is the advantage of using async with MVC5?

The async actions are useful only when you are performing I/O bound operations such as remote server calls. The benefit of the async call is that during the I/O operation, no ASP.NET worker thread is being used. So here’s how the first example works: When a request hits the action, ASP.NET takes a thread from … Read more

How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 [closed]

There is a brilliant blog post from Taiseer Joudeh with a detailed step-by-step description. Part 1: Token Based Authentication using ASP.NET Web API 2, Owin, and Identity Part 2: AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity Part 3: Enable OAuth Refresh Tokens in AngularJS App using ASP .NET Web API 2, … Read more

How to extend available properties of User.Identity

Whenever you want to extend the properties of User.Identity with any additional properties like the question above, add these properties to the ApplicationUser class first like so: public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, … Read more

How to add Web API to an existing ASP.NET MVC (5) Web Application project?

Update the MVC project Use Nuget to get the newest Web API. Project – Right click – Manage Nuget Packages – Search for Web API (Microsoft ASP.NET Web API …) and install it to your MVC project. Then you still need to get Web API routing to work. From Microsoft’s Configuring ASP.NET Web API 2 … Read more