在控制器中添加新代码后页面出现错误 404
Error 404 on page after adding new code in controller
我为一个新模型添加了一个新控制器及其视图,默认情况下一切正常,但是当我添加一些代码时,它破坏了页面,现在当我尝试打开它时,我收到错误 404。
如果删除该代码,我仍然会收到错误消息,并且无法恢复为默认设置。
我在另一个控制器上尝试了同样的事情,得到了同样的效果。
我尝试使用 Entity framework 和不使用创建控制器,结果相同。
我的其他控制器可以正常显示数据,但无法使用此控制器在数据库中创建数据。
不工作的控制器:
public class AccessController : Controller
{
private readonly DataAccess data;
public AccessController(DataAccess data)
{
this.data = data;
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AccessModel model)
{
if (ModelState.IsValid)
{
var CurrentUserName = this.User.FindFirstValue(ClaimTypes.Name);
List<ApplicationUser> users = new List<ApplicationUser>();
var userData = data.LoadUser();
string userID = null;
foreach (var row in userData)
{
users.Add(new ApplicationUser
{
MachineId = row.MachineId,
Email = row.Email
});
}
for (int j = 0; j < users.Count; j++)
{
if (users[j].Email == CurrentUserName)
userID = users[j].MachineId;
}
AccessProcessor.CreateAccess(userID, model.OpenValve, model.OpenTrap,
model.OpenFeedingPen, model.StartMachine, data);
}
return View();
}
}
启动时配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
appsettings.json:
"ConnectionStrings": {
"MVCCoreAppDB": "The connection string"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
那是因为你用HttpPost
装饰了你的Create
动作。您收到 404 错误的原因是因为您没有 Create
作为 Get
的操作。您所要做的就是添加另一个操作:
public ActionResult Create()
{
var model = new AccessModel();
return View(model);
}
我为一个新模型添加了一个新控制器及其视图,默认情况下一切正常,但是当我添加一些代码时,它破坏了页面,现在当我尝试打开它时,我收到错误 404。 如果删除该代码,我仍然会收到错误消息,并且无法恢复为默认设置。
我在另一个控制器上尝试了同样的事情,得到了同样的效果。
我尝试使用 Entity framework 和不使用创建控制器,结果相同。
我的其他控制器可以正常显示数据,但无法使用此控制器在数据库中创建数据。
不工作的控制器:
public class AccessController : Controller
{
private readonly DataAccess data;
public AccessController(DataAccess data)
{
this.data = data;
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AccessModel model)
{
if (ModelState.IsValid)
{
var CurrentUserName = this.User.FindFirstValue(ClaimTypes.Name);
List<ApplicationUser> users = new List<ApplicationUser>();
var userData = data.LoadUser();
string userID = null;
foreach (var row in userData)
{
users.Add(new ApplicationUser
{
MachineId = row.MachineId,
Email = row.Email
});
}
for (int j = 0; j < users.Count; j++)
{
if (users[j].Email == CurrentUserName)
userID = users[j].MachineId;
}
AccessProcessor.CreateAccess(userID, model.OpenValve, model.OpenTrap,
model.OpenFeedingPen, model.StartMachine, data);
}
return View();
}
}
启动时配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
appsettings.json:
"ConnectionStrings": {
"MVCCoreAppDB": "The connection string"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
那是因为你用HttpPost
装饰了你的Create
动作。您收到 404 错误的原因是因为您没有 Create
作为 Get
的操作。您所要做的就是添加另一个操作:
public ActionResult Create()
{
var model = new AccessModel();
return View(model);
}