- Details
- Written by: Stanko Milosev
- Category: Core
- Hits: 2347
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Radius { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseRouting(); app.UseCors(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
- Details
- Written by: Stanko Milosev
- Category: Core
- Hits: 1980
<!DOCTYPE html> <head> <meta charset="utf-8" /> <script defer type="text/javascript" src="lib/jquery-3.5.1.js"></script> <script defer type="text/javascript" src="js/index.js"></script> </head> <body> <button id="button" type="button">Click Me!</button> </body>index.js:
$( "#button" ).click(function() { var json = JSON.stringify({"cities": ["Aurel Vlaicu", "Asquins", "Arnoldstein"]}); $.ajax({ url: "api/Empty/", type: "POST", data: json, dataType: "json", contentType: "application/json; charset=utf-8", success: function (data) { alert("Success."); } }).done(function (data) { try { json = $.parseJSON( JSON.stringify(data)); } catch (e) { alert("Error not JSON. " + e.message) } }).error(function (xhr, ajaxOptions, thrownError) { alert("Error."); }).fail(function (data) { alert("Sorry. Server unavailable. "); }); });.NET side:
[HttpPost] public ActionResultNotice that in "$.ajax.done" I added try..catch block, and JSON.stringify because of dataType: "json" jQuery will automatically parse string into JSON.Get([FromBody] Filter filter) { string returnValue = string.Empty; foreach (var city in filter.cities) { returnValue = $"{city}, {returnValue}"; } return Ok(JsonSerializer.Serialize(returnValue)); }
- Details
- Written by: Stanko Milosev
- Category: Core
- Hits: 2545
<!DOCTYPE html> <head> <meta charset="utf-8" /> <script defer type="text/javascript" src="lib/jquery-3.5.1.js"></script> <script defer type="text/javascript" src="js/index.js"></script> </head> <html xmlns="http://www.w3.org/1999/xhtml"> <button id="button" type="button">Click Me!</button> </html>index.js:
$( "#button" ).click(function() { var json = JSON.stringify({"cities": ["Aurel Vlaicu", "Asquins", "Arnoldstein"]}); $.ajax({ url: "api/Empty/", type: "POST", data: json, contentType: "application/json; charset=utf-8" }).done(function (data) { alert("Success."); }).fail(function (data) { alert("Sorry. Server unavailable. "); }); });Asp.Net core API:
using Microsoft.AspNetCore.Mvc; namespace AspDotNetCorePostExample.Controllers { [Route("api/[controller]")] [ApiController] public class EmptyController : ControllerBase { [HttpGet] public string Get() { return "Hello world"; } [HttpPost] public string Get([FromBody] Filter filter) { string returnValue = string.Empty; foreach (var city in filter.cities) { returnValue = $"{city}, {returnValue}"; } return returnValue; } public class Filter { public string[] cities { get; set; } } } }Notice in jQuery that in Ajax call I didn't add dataType: "json" since my API returns string and not JSON. In API notice [FromBody] in method signature, and Filter class which is defined exactly the same as JSON var json = JSON.stringify({"cities": ["Aurel Vlaicu", "Asquins", "Arnoldstein"]}); which I am sending to API. Example download from here
- Details
- Written by: Stanko Milosev
- Category: Core
- Hits: 2481






using Microsoft.AspNetCore.Mvc; namespace AspDotNetCorePostExample.Controllers { [Route("api/[controller]")] [ApiController] public class EmptyController : ControllerBase { [HttpGet] public string Get() { return "Hello world"; } } }In AspDotNetCorePostExample\AspDotNetCorePostExample\Startup.cs in method "Configure" delete:
app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); });Instead write:
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });In ConfigureServices add services.AddControllers(); Start solution and go to like "https://localhost:44336/api/Empty" (just add /api/Empty) If you like you can manually add wwwroot folder, and use him like I already explained here. Here is new Startup.cs, and to disable HTPPS go to project properties -> Debug -> uncheck "Enable SSL":
