ASP.NET Core Razor 无法从 javascript ajax 调用访问控制器

问题描述

我有以下 javascript 代码

<script>
function mergeQuotes() {
        var grid = $("#quotesgrid").dxDataGrid("instance");
        var data = grid.getSelectedRowsData();

        //var text = s.document.getText();
        //var encodedString = window.btoa(text); //encodeURIComponent(text);

        var dummy = window.btoa("not used");
        alert("Made it to mergeQuotes");

        $.ajax({
            url: "api/home/exporttodb",type: "POST",data: { base64: dummy,quoteData: data,preview: true}
        });
        DevExpress.ui.notify("Preparing Preview email(s)...");
    }
</script>

我有以下控制器操作:

    [Route("api/[controller]/[action]")]
public class HomeController : Controller
{
    private pqContext _context;
    private readonly IEmailService _Mailer;

    public HomeController(pqContext context,IEmailService Mailer)
    {
        _context = context;
        _Mailer = Mailer;

    }
    public IActionResult Index()
    {
        return View();
    }

    public void OnPostExport(string base64,string fileName,DevExpress.AspNetCore.RichEdit.DocumentFormat format)
    {
        byte[] fileContents = System.Convert.FromBase64String(base64);
    }

    [HttpPost]
    [Route("pq/exporttodb")]
    public IActionResult ExportToDB(string base64,List<vQuoteCombos> quoteData,bool preview)
    {
        // Need to reach here...
    }
}

我的页面是这样装饰的:

@page "pq"
@using lykins_pitstop_lob_pq.Models
@using lykins.pitstop.lob.pq.models

<style>
    .options {
        padding: 20px;
        background-color: rgba(191,191,0.15);
        margin-top: 20px;
    }
</style>

<div class="text-center">
    <h1>Lykins Price Quoting Tool (PQT) Home</h1>
    @*@(Html.DevExtreme().Button().Text("How To").Icon("fa fa-question")
            .OnClick("goToHelp")
                    )*@
</div>

<!--  More stuff happens but it is not germaine to this issue and the java script is at the bottom of this  page --!>

<script>
  function ExportToDb(){
  }
<script>

这个项目是依赖注入到我的主项目的启动中,我在主项目中配置了启动文件

    public class Startup
{
    private PQStartup pqStartup { get; set; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        pqStartup = new PQStartup(configuration);
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UsesqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDatabaseDeveloperPageExceptionFilter();
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddRazorPages()
            .AddJsonoptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
        services.AddMvc().AddRazorPagesOptions(o =>
        {
            o.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
        });

        services.AddAuthentication()
                .AddMicrosoftAccount(microsoftOptions =>
                {
                    microsoftOptions.ClientId = "XXXXXXX";
                    microsoftOptions.ClientSecret = "XXXXXXXX";
                });


        // Add LOB Services here //
        pqStartup.ConfigureServices(services);
    }

    // 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.UseMigrationsEndPoint();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"node_modules")),RequestPath = "/node_modules"
        });

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        // ****************  Add LOB apps Here ***********************//
        pqStartup.Configure(app,env);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapRazorPages();
        });
    }
}

当我调试这个项目时,我不断在边缘控制台中收到以下 400 或 500 错误,具体取决于我对此现有代码所做的更改。如何从 javascript 引用此控制器操作以访问它并执行其操作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)