Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Elizabeth Thompson 3 posts 23 karma points
    Jun 27, 2021 @ 19:11
    Elizabeth Thompson

    Enable cors on umbraco 9

    I've set up a .net core application with umbraco 9 and created an api controller. I want to use a different app to hit the api endpoint but I receive this message:

    System.InvalidOperationException: Endpoint UmbracoNine.Controllers.HomeController.GetHomeHeading (UmbracoNine) contains CORS metadata, but a middleware was not found that supports CORS. Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. The call to app.UseCors() must appear between app.UseRouting() and app.UseEndpoints(...). at Microsoft.AspNetCore.Routing.EndpointMiddleware.ThrowMissingCorsMiddlewareException(Endpoint endpoint) at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) at Umbraco.Cms.Web.Website.Middleware.PublicAccessMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>cDisplayClass6_1.<1>d.MoveNext()

    I followed the instructions by creating a cors policy in the startup.cs configure services:

    services.AddCors(options => { options.AddPolicy(corsPolicy, builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); });

    and calling app.UserCors(corsPolicy) in the startup.cs configure. But I still receive the same message on the api controller. How can I fix this in .net core with umbraco 9 api controller?

  • Benjamin Carleski 33 posts 291 karma points MVP c-trib
    Jun 28, 2021 @ 22:37
    Benjamin Carleski

    The key part is The call to app.UseCors() must appear between app.UseRouting() and app.UseEndpoints(...). Umbraco makes both those calls for you, so you can't just add the UseCors() call directly to the Startup.cs file, otherwise it won't appear between those two calls.

    Fortunately Umbraco does offer the IUmbracoPipelineFilter interface to handle cases such as this. It lets you tap into the startup pipeline between key points. There isn't any documentation I could find, but when adding CORS to the v9 version of the GraphQL package, I was able to work through it enough to get it working.

    What you do is add a class that extends UmbracoPipelineFilter and implement the PostPipeline method to make the call. You can see an example of that here. You'll need to add an IConfigureOptions<UmbracoPipelineOptions> implementation to register you filter, as seen here. Then you'll need to reference that class in the service registration using the ConfigureOptions extension method, as seen here.

    Finally, if you want to customize the policy at all, you will also need to add your CORS policy before the UseCors call, either in a composer or earlier in the Startup method. More details on that can be find in the Microsoft documentation.

  • Elizabeth Thompson 3 posts 23 karma points
    Jun 29, 2021 @ 21:36
    Elizabeth Thompson

    That worked! Thank you so much for the help

  • Shih-Chan Fan 4 posts 24 karma points
    Nov 07, 2021 @ 03:12
    Shih-Chan Fan

    Finally, I have had solved the CORS problem. I make the record one here.

    1. Create Filter enter image description here
    2. Create UmbracOptions enter image description here
    3. Create Composer enter image description here
    4. Modify Startup enter image description here
Please Sign in or register to post replies

Write your reply to: