![](/img/trans.png)
[英].NET Core 2.0 MVC odd 404 on controller methods only when deployed to IIS
[英](.net core MVC) Model is being returned null after form post but only when deployed to on premise IIS server
所以我遇到了這個非常奇怪的問題,我的 model 在提交表單后返回為 null。
現在我明白了 model 返回 null 本身並不一定很奇怪,但讓它非常奇怪的是它只發生在非常特殊的條件下:
我一輩子都弄不明白為什么會這樣,因為當我在本地運行時,一切正常。 但是,一旦我在本地服務器上部署並修改了頁面的日期過濾器,我就開始收到此錯誤。
這是我的觀點:
@model CoFCrimeBulletin.Models.ViewModels.CallsForServiceViewModel
@{
ViewData["Title"] = "Calls For Service";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div style="margin-left: 400px; width: 100%">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Arrests">Arrests</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Warrants">Warrants</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="StreetChecks">Street Checks</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark active" asp-area="" asp-controller="Home" asp-action="CallsForService">Calls For Service</a>
</li>
</ul>
</div>
<div class="container pt-5 CallsForService" >
<form asp-controller="Home" asp-action="CallsForService" method="post">
<div class=row>
<div class="pr-2">
<label><b>Search: </b></label>
<input type="text" id="WarrantsSearch" onkeyup="myWarrantsFunction()" placeholder="Search Calls for Service">
</div>
<div class="pr-2">
<label><b>Start Date: </b></label>
<input id="CallsForServiceStartDate" type="date" asp-for="@Model.StartDate" class="text-box single-line" value="@Model.StartDate.ToString("yyyy-MM-dd")" />
</div>
<div class="pr-2">
<label><b>End Date: </b></label>
<input id="CallsForServiceEndDate" type="date" asp-for="@Model.EndDate" class="text-box single-line" value="@Model.EndDate.ToString("yyyy-MM-dd")" />
</div>
<div>
<button type="submit" class="btn btn-secondary" onclick="VerifyDates()">Go</button>
</div>
</div>
</form>
<div class="row ">
<div class="col-sm-4 tableheading"><b>LOCAL CRIME</b></div>
</div>
<div class="row details">
<div class="col-sm" style="text-align:center"><b>Details</b></div>
</div>
<form asp-controller="Home" asp-action="CreatePDF" method="post">
<div id="LocalCrimeTable">
@{
for (int i = 0; i < Model.callsForService.Count; i++)
{
<div class="row suspectName">
<div class="col-sm-9">
<b>@Model.callsForService[i].type - @Model.callsForService[i].location</b>
<input asp-for="@Model.callsForService[i].location" style="display:none;" value="@Model.callsForService[i].location">
<input asp-for="@Model.callsForService[i].type" style="display:none;" value="@Model.callsForService[i].type">
</div>
<div class="col-sm-3" style="text-align:right">
<b>@Model.callsForService[i].datetime</b>
<input type="date" asp-for="@Model.callsForService[i].datetime" style="display:none;" value="@Model.callsForService[i].datetime.ToString("yyyy-MM-dd")">
</div>
</div>
<div class="row rmsNumber">
<div class="col-sm-4">
<b>@Model.callsForService[i].rmsnumdisplay</b>
<input asp-for="@Model.callsForService[i].rmsnumdisplay" style="display:none;" value="@Model.callsForService[i].rmsnumdisplay">
<input asp-for="@Model.callsForService[i].rmsnum" style="display:none;" value="@Model.callsForService[i].rmsnum">
</div>
<div class="col-sm-4">
<b>@Model.callsForService[i].zone</b>
<input asp-for="@Model.callsForService[i].zone" style="display:none;" value="@Model.callsForService[i].zone">
</div>
<div class="col-sm-4">
<b>COMPLAINTANT:</b> @Model.callsForService[i].caller
<input asp-for="@Model.callsForService[i].caller" style="display:none;" value="@Model.callsForService[i].caller">
</div>
</div>
<div class="row">
<div class="col-sm">
<b>REMARKS:</b> @Model.callsForService[i].remarks
<input asp-for="@Model.callsForService[i].remarks" style="display:none;" value="@Model.callsForService[i].remarks">
</div>
</div>
<div class="row">
<div class="col-sm">
<b>CLEAR REMARKS:</b> @Model.callsForService[i].clearremarks
<input asp-for="@Model.callsForService[i].clearremarks" style="display:none;" value="@Model.callsForService[i].clearremarks">
</div>
</div>
if(Model.IsWriter)
{
<div id="td">
<a href="#" class="btn btn-secondary additionalInfoEdit" data-target="#additionalInfoModal" data-toggle="modal" onclick="additionalInfoRender('@Model.callsForService[i].rmsnum', '@ViewData["Title"]', '@Model.StartDate.ToString()', '@Model.EndDate.ToString()')">+</a>
</div>
}
<div class="row">
<div class="col-12"> <b>ADDITIONAL NOTES:</b>
@if(Model.callsForService[i].additionalnotes != null && Model.callsForService[i].additionalnotes != ""){
<p class="truncateCrime">@Model.callsForService[i].additionalnotes</p>
<a class="expandNotesCrime underline text-green-500 button" href="#">Read more...</a>
<a class="shrinkNotesCrime underline text-green-500 button" href="#" style="display:none">Read less...</a>
}
<input asp-for="@Model.callsForService[i].additionalnotes" style="display:none;" value="@Model.callsForService[i].additionalnotes">
</div>
</div>
<div class="row" style="text-transform:capitalize">
<input asp-for="@Model.callsForService[i].createPDF" type="checkbox"> Check to Include in PDF
</div>
}
<input type="date" asp-for="@Model.StartDate" style="display:none;" value="@Model.StartDate.ToString("yyyy-MM-dd")">
<input type="date" asp-for="@Model.EndDate" style="display:none;" value="@Model.EndDate.ToString("yyyy-MM-dd")">
<input asp-for="@Model.SourcePage" style="display:none;" value="@Model.SourcePage">
}
</div>
@{
<input type="submit" class="btn btn-primary mt-5" value="Create PDF">
}
</form>
</div>
還有我的 Controller:
using CoFCrimeBulletin.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using System.Security.Claims;
namespace CoFCrimeBulletin.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public IConfiguration _configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
private bool IsWriter()
{
bool localtest = false;
if (localtest) { return true; }
else
{
//ClaimsIdentity userIdentity = (ClaimsIdentity)User.Identity;
var UI = (ClaimsIdentity)User.Identity;
List<string> Groups = new List<string>();
if (UI.Claims.Where(c => c.Type == "groups").Count() > 0)
{
foreach (var group in UI.Claims.Where(c => c.Type == "groups"))
{
Groups.Add(group.Value);
}
}
return Groups.Contains("f615ae09-b313-444a-a41c-5dc2d586fc03");
}
}
public IActionResult Index(Models.ViewModels.BaseViewModel model)
{
Models.ViewModels.HomePageViewModel myModel = new Models.ViewModels.HomePageViewModel();
Repository.WarrantRepository warrantRepository = new Repository.WarrantRepository(_configuration);
Repository.ArrestRepository arrestRepository = new Repository.ArrestRepository(_configuration);
Repository.CrimeRepository crimeRepository = new Repository.CrimeRepository(_configuration);
DateTime startDate = model.StartDate;
DateTime endDate = model.EndDate;
myModel.warrants = warrantRepository.GetWarrants(startDate, endDate);
myModel.arrests = arrestRepository.GetArrests(startDate, endDate);
myModel.streetChecks = crimeRepository.GetStreetChecks(startDate, endDate);
myModel.callsForService = crimeRepository.GetCallsForService(startDate, endDate);
myModel.StartDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-1) : startDate;
myModel.EndDate = endDate == DateTime.MinValue ? DateTime.Now : endDate;
myModel.IsWriter = IsWriter();
myModel.SourcePage = "Index";
return View(myModel);
}
public IActionResult Warrants(Models.ViewModels.BaseViewModel model)
{
DateTime startDate = model.StartDate;
DateTime endDate = model.EndDate;
Models.ViewModels.WarrantViewModel myModel = new Models.ViewModels.WarrantViewModel();
myModel.StartDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-1) : startDate;
myModel.EndDate = endDate == DateTime.MinValue ? DateTime.Now : endDate;
Repository.WarrantRepository warrantRepository = new Repository.WarrantRepository(_configuration);
myModel.Warrants = warrantRepository.GetWarrants(startDate, endDate);
myModel.IsWriter = IsWriter();
myModel.SourcePage = "Warrants";
return View(myModel);
}
public IActionResult Arrests(Models.ViewModels.BaseViewModel model)
{
DateTime startDate = model.StartDate;
DateTime endDate = model.EndDate;
Models.ViewModels.ArrestViewModel myModel = new Models.ViewModels.ArrestViewModel();
myModel.StartDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-1) : startDate;
myModel.EndDate = endDate == DateTime.MinValue ? DateTime.Now : endDate;
Repository.ArrestRepository arrestRepository = new Repository.ArrestRepository(_configuration);
myModel.arrests = arrestRepository.GetArrests(startDate, endDate);
myModel.IsWriter = IsWriter();
myModel.SourcePage = "Arrests";
return View(myModel);
}
public IActionResult StreetChecks(Models.ViewModels.BaseViewModel model)
{
DateTime startDate = model.StartDate;
DateTime endDate = model.EndDate;
Models.ViewModels.StreetChecksViewModel myModel = new Models.ViewModels.StreetChecksViewModel();
myModel.StartDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-1) : startDate;
myModel.EndDate = endDate == DateTime.MinValue ? DateTime.Now : endDate;
Repository.CrimeRepository crimeRepository = new Repository.CrimeRepository(_configuration);
myModel.streetChecks = crimeRepository.GetStreetChecks(startDate, endDate);
myModel.IsWriter = IsWriter();
myModel.SourcePage = "StreetChecks";
return View(myModel);
}
public IActionResult CallsForService(Models.ViewModels.BaseViewModel model)
{
DateTime startDate = model.StartDate;
DateTime endDate = model.EndDate;
Models.ViewModels.CallsForServiceViewModel myModel = new Models.ViewModels.CallsForServiceViewModel();
myModel.StartDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-1) : startDate;
myModel.EndDate = endDate == DateTime.MinValue ? DateTime.Now : endDate;
Repository.CrimeRepository crimeRepository = new Repository.CrimeRepository(_configuration);
myModel.callsForService = crimeRepository.GetCallsForService(startDate, endDate);
myModel.IsWriter = IsWriter();
myModel.SourcePage = "CallsForService";
return View(myModel);
}
public IActionResult AddAdditionalNote(Models.ViewModels.AdditionalInfoViewModel model)
{
try
{
Repository.AdditionalNotesRepository additionalNotesRepository = new Repository.AdditionalNotesRepository(_configuration);
string redirectAction = Helpers.AdditionalInfoHelper.GetRedirectActionName(model.sourcePage);
Models.ViewModels.BaseViewModel myModel = new Models.ViewModels.BaseViewModel();
string rmsNum = model.rmsNum.Replace("-", string.Empty);
string remarks = model.additionalNotes == null? "" : model.additionalNotes;
myModel.StartDate = model.StartDate;
myModel.EndDate = model.EndDate;
additionalNotesRepository.InsertAdditionalNotes(rmsNum, remarks);
return RedirectToAction(redirectAction, myModel);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return Redirect(HttpContext.Request.Headers["Referer"]);
}
public PartialViewResult RenderAdditionalInfoModal(string RmsNum, DateTime StartDate, DateTime EndDate, string SourcePage)
{
string rms = RmsNum.Replace("-", string.Empty);
Repository.AdditionalNotesRepository additionalNotesRepository = new Repository.AdditionalNotesRepository(_configuration);
Models.ViewModels.AdditionalInfoViewModel myModel = new Models.ViewModels.AdditionalInfoViewModel();
myModel.rmsNum = RmsNum;
myModel.StartDate = StartDate;
myModel.EndDate = EndDate;
myModel.sourcePage = SourcePage;
myModel.additionalNotes = additionalNotesRepository.GetAdditionalNote(rms);
return PartialView("_AdditionalInfoPartial", myModel);
}
[HttpPost]
public IActionResult CreatePDF(Models.ViewModels.HomePageViewModel model)
{
try
{
Models.ViewModels.BaseViewModel myModel = new Models.ViewModels.BaseViewModel();
Models.PdfCreation pdf = new Models.PdfCreation();
string redirectAction = model.SourcePage;
redirectAction = Helpers.AdditionalInfoHelper.GetRedirectActionName(model.SourcePage);
myModel.StartDate = model.StartDate;
myModel.EndDate = model.EndDate;
pdf.arrests = Helpers.PdfHelper.GetArrestsForPdfCreation(model.arrests);
pdf.warrants = Helpers.PdfHelper.GetWarrantsForPdfCreation(model.warrants);
pdf.callsForService = Helpers.PdfHelper.GetCallsForServiceForPdfCreation(model.callsForService);
pdf.streetChecks = Helpers.PdfHelper.GetStreetChecksForPdfCreation(model.streetChecks);
Helpers.PdfHelper.CreatePdf(pdf);
return RedirectToAction(redirectAction, myModel);
}
catch (Exception ex)
{
using (StreamWriter writer = new StreamWriter(@"C:\CrimeBulletin PDFs\out.txt", true))
{
Console.SetOut(writer);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
return Redirect(HttpContext.Request.Headers["Referer"]);
}
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult Privacy()
{
return View();
}
}
}
和型號:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoFCrimeBulletin.Models.ViewModels
{
public class HomePageViewModel : BaseViewModel
{
public List<Arrest> arrests { get; set; }
public List<Warrant> warrants { get; set; }
public List<Crime> callsForService { get; set; }
public List<Crime> streetChecks { get; set; }
public HomePageViewModel()
{
arrests = new List<Arrest>();
warrants = new List<Warrant>();
callsForService = new List<Crime>();
streetChecks = new List<Crime>();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace CoFCrimeBulletin.Models.ViewModels
{
public class CallsForServiceViewModel : BaseViewModel
{
public List<Crime> callsForService { get; set; }
public CallsForServiceViewModel()
{
callsForService = new List<Crime>();
}
}
}
當我提交表單時,顯示 model 的屏幕截圖是 null:
如果我不修改日期過濾器,屏幕截圖顯示 model 實際上正在填充:
重要的是要注意,這兩個屏幕截圖都顯示了提交相同表單的結果。 還要指出的是,這些屏幕截圖是從服務器上的遠程調試中獲取的。 不是來自我的本地機器。
同樣重要的是要注意,當我在我的本地機器上運行它時,我可以提交任何我想要的方式,包括在修改日期過濾器之后,我永遠不會得到 null model 回來。 當應用程序部署到我的本地 IIS 服務器時,我只能復制此錯誤。
我找不到與我遇到的問題相匹配的任何其他帖子。 任何幫助都將不勝感激。
由於我將屏幕上顯示的所有數據回發到我的 controller,然后從那里進行排序,問題可能是當太多數據被發布到 controller model 時,model 是 null 嗎? 目前正在調查此事。
好的,我發現並解決了問題。 它與部署到內部部署的 IIS 服務器無關,也與我正在更改日期范圍以及與我回發到 controller 的數據集大小有關。
由於我將所有數據發回我的 controller,然后根據復選框值進行排序,發回我的 controller 的數據量超過了 asp.net 核心 MVC 設置的默認限制。
現在人們可能會猜到,我在本地使用的數據集足夠小,不會超過限制。 此外,增加我的過濾器的日期范圍會產生使數據集更大的次要效果。
解決方案:
我很難識別此錯誤的原因是因為我只收到 nullReference 錯誤,而沒有收到“InvalidDataException:超出表單值計數限制 1024”。
其中,一旦我確定問題將我帶到這個線程,它告訴我你可以用這段代碼覆蓋默認限制:
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
});
在 Startup.cs 的 ConfigureServices 方法中
我添加了那段代碼,問題就消失了。 model 現在可以正確綁定大量數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.