首页 > 解决方案 > ASP.NET Core Web 应用程序无法实时运行,但可以在本地运行

问题描述

我正在将我的应用程序部署到 Azure,但问题是某些方法,特别是 POST 方法在本地工作时会在实时站点上给出 404。我一直在尝试使用 BurpSuite 对其进行调试,但似乎请求是相似的。

本地主机

现场直播:

控制器:

[HttpPost]
public IActionResult SavePlan(string PlanDate)
{
    DateTime dateFrom = DateTime.ParseExact(PlanDate, "dd/MM/yyyy", null);

    // Get planCart
    PlanCart planCart = GetPlanCart();

    // Validate MealPlan
    if (!ValidateMealPlan(planCart))
    {
        TempData["Error"] = "Error: MealPlan contains has too many diet restrictions per day.";
        return RedirectToAction("Index");
    }

    // Create and set MealPlan options
    MealPlan mealPlan = new MealPlan();
    mealPlan.dateFrom = dateFrom;
    mealPlan.dateTo = dateFrom.AddDays(7);
    mealPlan.Meals = planCart.returnList().ToArray();

    mealplanRepository.SaveMealPlan(mealPlan);

    return RedirectToAction("Index");
}

启动.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = 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)
    {
        // Add Identity server
        services.AddDbContext<AppIdentityDbContext>(options =>
            options.UseSqlServer(
            Configuration["Data:EasyMealIdentityServer:ConnectionString"]));

        // Add OrderCustomersServer
        services.AddDbContext<AppMealOrdersCustomersDbContext>(options =>
            options.UseSqlServer(
            Configuration["Data:EasyMealOrdersCustomersServer:ConnectionString"]));

        // EasyMealMealServer
        services.AddDbContext<AppMealsDbContext>(options =>
            options.UseSqlServer(
            Configuration["Data:EasyMealMealServer:ConnectionString"]));

        services.AddTransient<IMealRepository, EFMealRepository>();
        services.AddTransient<IOrderRepository, EFOrderRepository>();
        services.AddTransient<IMealplanRepository, EFMealplanRepository>();

        services.AddIdentity<AppUser, IdentityRole>()
         .AddEntityFrameworkStores<AppIdentityDbContext>()
         .AddDefaultTokenProviders();

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddMemoryCache();
        services.AddSession();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseAuthentication();
        app.UseSession();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseCookiePolicy();
    }
}

视图中的表单(Index.cshtml):

        <form id="PlanForm" asp-action="SavePlan" asp-controller="Plan" method="post">
        <input id="PlanDate" name="PlanDate" value="" type="hidden" />
        <button class="btn btn-success" type="submit">Save selection</button>
    </form>

PlanDate,所需的参数通过 javascript 设置。您可以在 BurpSuite 的请求中看到它确实被发送了。我想也许这就是问题所在。

如果有人知道可能出了什么问题,将不胜感激!

标签: c#asp.net-core

解决方案


**编辑 2:您需要格式化日期客户端并使用 clientinfo 服务器端以避免 mm/dd/yyyy vs dd/mm/yyyy 不匹配!

编辑1:您是否在日期中传递正斜杠?那会破坏它。用破折号代替日期或在javascript中转义它。**

右键单击项目,单击属性,单击 web 选项卡,向下滚动以查看正确的带有端口的 iis url。

对于 .net 核心,使用 launchSettings.json 并找到应用程序 url。

当您尝试发布到它时,请确保您正在做localhost:<port>\<controller>\<action>

所以控制器可能在家,动作是保存计划,之后你需要 \plandate


推荐阅读