locked
MapPost endpoint routing RRS feed

  • Question

  • User853640082 posted
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
        endpoints.MapPost("{controller=Home}/{action=Index}/{id?}", -----------);
        endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
    });


    I want to invoke a controller action in blazor server-side project, i feel i need to specify the MapPost endpoint routing in startup.cs, but however i don't know the correct argument to the second parameter of the MapPost method. Any help to solve this problem is greatly appreciated. Thanks

    Tuesday, October 1, 2019 9:25 PM

Answers

  • User283571144 posted

    Hi PeterAyobami,

    As far as I know, the  endpoints.MapPost is used for mapping a POST endpoint on the {controller=Home}/{action=Index}/{id?} path.

    Notice:  this is used to handle the request which is post to the special path. You should write your own delegate into it.

    For exmaple:

    	endpoints.MapPost("{controller=Home}/{action=Index}/{id?}", async context =>
    	{
    	    var cancellationToken = context.RequestAborted;
    
    	    var model = await ReadModelAsync(context.Request.BodyReader, cancellationToken);
    
    	    context.Response.ContentType = "text/plain";
    	    context.Response.Headers[HeaderNames.CacheControl] = "no-cache";
    
    	    await context.Response.StartAsync(cancellationToken);
    
    	    await WriteUrlAsync(context.Response.BodyWriter, model, cancellationToken);
    	})

    Within the RequestDelegate we have access to the HttpContext.

    We first store a local variable which accessed the RequestAborted property on the HttpContext . This is a CancellationToken which allows us to check whether the caller has aborted the request that we’re currently handling. By passing the cancellation token around, we can cancel any work we’re doing at the appropriate time.

    The next step is to attempt to read the JSON payload from the BodyReader to populate a model object. This code is defined in a private method called ReadModelAsync.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 7:19 AM

All replies

  • User283571144 posted

    Hi PeterAyobami,

    As far as I know, the  endpoints.MapPost is used for mapping a POST endpoint on the {controller=Home}/{action=Index}/{id?} path.

    Notice:  this is used to handle the request which is post to the special path. You should write your own delegate into it.

    For exmaple:

    	endpoints.MapPost("{controller=Home}/{action=Index}/{id?}", async context =>
    	{
    	    var cancellationToken = context.RequestAborted;
    
    	    var model = await ReadModelAsync(context.Request.BodyReader, cancellationToken);
    
    	    context.Response.ContentType = "text/plain";
    	    context.Response.Headers[HeaderNames.CacheControl] = "no-cache";
    
    	    await context.Response.StartAsync(cancellationToken);
    
    	    await WriteUrlAsync(context.Response.BodyWriter, model, cancellationToken);
    	})

    Within the RequestDelegate we have access to the HttpContext.

    We first store a local variable which accessed the RequestAborted property on the HttpContext . This is a CancellationToken which allows us to check whether the caller has aborted the request that we’re currently handling. By passing the cancellation token around, we can cancel any work we’re doing at the appropriate time.

    The next step is to attempt to read the JSON payload from the BodyReader to populate a model object. This code is defined in a private method called ReadModelAsync.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 7:19 AM
  • User853640082 posted

    Thanks a lot Brando, this was helpful. But however as i've tried this i'm not still hitting this post request.

    Do you have any other clues to this: on hitting POST requests in server-side blazor?

    Wednesday, October 2, 2019 11:16 AM