locked
call action from another project in same solution RRS feed

  • Question

  • User-1952516322 posted

    Hello,

    I have two project in one solution ( ASP CORE ), I have an action in first project 

        [Route("[controller]/[action]")]
    public class HomeController : Controller
    {
    [HttpGet] public string Index() { return "Hi"; }
    }

    I tried this in second project , but its not working.

    Note: By the way, I did dependecy

    $.ajax({
                        url: 'https://localhost:xxx/Home/Index',
                        type: 'get',
                        dataType: 'json',
                        contentType: 'application/json;charset=utf-8',
                        success: function (data) {
                            alert('Success: ' + data);
                        },
                        error: function (error) {
                            alert(error.statusText);
                        }
                    });

    I wrote this code also in Startup.cs

    // ConfigureServices
    services.AddCors(options =>
                {
                    options.AddPolicy("AnyOrigin", builder =>
                     {
                         builder
                         .AllowAnyOrigin()
                         .AllowAnyMethod();
                     });
                });
    
    
    
    
    // Configure
    
     app.UseCors("AnyOrigin");

    Tuesday, April 16, 2019 6:34 AM

Answers

  • User-893317190 posted

    Hi Khalid Salameh,

    Since you are sending contentType header to server, you should also AllowAnyHeader.

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddCors(options =>
                {
                    options.AddPolicy("AnyOrigin", builder =>
                    {
                        builder
                        .AllowAnyOrigin()
                         .AllowAnyHeader()
                        .AllowAnyMethod();
                    });
                });
                services.AddRouting();
                services.AddMvc();
                
    
            }

    Also please put app.UseCors("AnyOrigin") in front of app.UseMvc.

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                app.UseCors("AnyOrigin");
                if (env.IsDevelopment())
                {
                    app.UseBrowserLink();
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseStaticFiles();
                
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }

    My controller.

     [Route("/[controller]/[action]/hello")]
            public IActionResult Json()
            {
                return Json(new { Message = "hi" });
    
                
            }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2019 4:40 AM

All replies

  • User-893317190 posted

    Hi Khalid Salameh,

    Since you are sending contentType header to server, you should also AllowAnyHeader.

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddCors(options =>
                {
                    options.AddPolicy("AnyOrigin", builder =>
                    {
                        builder
                        .AllowAnyOrigin()
                         .AllowAnyHeader()
                        .AllowAnyMethod();
                    });
                });
                services.AddRouting();
                services.AddMvc();
                
    
            }

    Also please put app.UseCors("AnyOrigin") in front of app.UseMvc.

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                app.UseCors("AnyOrigin");
                if (env.IsDevelopment())
                {
                    app.UseBrowserLink();
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseStaticFiles();
                
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }

    My controller.

     [Route("/[controller]/[action]/hello")]
            public IActionResult Json()
            {
                return Json(new { Message = "hi" });
    
                
            }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2019 4:40 AM
  • User-1952516322 posted

    Thanks, Its working, the problem was 

    AllowAnyHeader not added, also should app.UseCors("AnyOrigin") on the top.... 

    Thanks

    Wednesday, April 17, 2019 10:57 AM