HTTPPOST返回页面找不到HTTP错误405 ASP.NET Core MVC IIS Express
当我放入控制器httppost时,我对表单有问题,错误http 405它向我显示我的
每个步骤,我使其正确的
代码
@model security.Models.ContactUs
<div class="contact_box">
<form id="Contact" asp-controller="Home" asp-action="Contact" method="POST">
<input type="text" asp-for="Name" placeholder="Your Name">
<input type="email" asp-for="Email" placeholder="Email">
<input type="text" asp-for="PhoneNumber" placeholder="Phone Number">
<input type="text" asp-for="Text" placeholder="Message">
<button type="submit">Contact Us</button>
</form>
</div>
和控制器
namespace security.Controllers
{
public class HomeController : Controller
{
DBContext db;
public HomeController(DBContext context)
{
db = context;
}
//private readonly ILogger<HomeController> _logger;
//public HomeController(ILogger<HomeController> logger)
//{
// _logger = logger;
//}
[HttpGet]
public IActionResult Index()
{
CollectionsData model = new CollectionsData();
model.Offers = GetOffers();
model.Services = GetServices();
model.News = GetNews();
model.Team = GetSecurityTeam();
return View(model);
}
[HttpPost]
public IActionResult Contact(ContactUs model)
{
db.Contactus.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
public IActionResult Services( Sevices model)
{
var ModelServices = db.Sevices.ToList();
return View(ModelServices);
}
//[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
//public IActionResult Error()
//{
// return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
//}
public IEnumerable<Offers> GetOffers()
{
return db.Offers.ToList();
}
public IEnumerable<Sevices> GetServices()
{
return db.Sevices.ToList();
}
public IEnumerable<ourNews> GetNews()
{
return db.News.ToList();
}
public IEnumerable<SecurityTeam> GetSecurityTeam()
{
return db.Team.ToList();
}
}
}
正确,但是当我删除httppost时,一切都很好,除了将表单发送到数据库,
我不这样做为什么该页面不加载它的给出HTTP错误405
谢谢
i have problem with form when i put in my Controller httpPost the Error http 405 it showing to me
every step i made its correct
my code
@model security.Models.ContactUs
<div class="contact_box">
<form id="Contact" asp-controller="Home" asp-action="Contact" method="POST">
<input type="text" asp-for="Name" placeholder="Your Name">
<input type="email" asp-for="Email" placeholder="Email">
<input type="text" asp-for="PhoneNumber" placeholder="Phone Number">
<input type="text" asp-for="Text" placeholder="Message">
<button type="submit">Contact Us</button>
</form>
</div>
and my Controller
namespace security.Controllers
{
public class HomeController : Controller
{
DBContext db;
public HomeController(DBContext context)
{
db = context;
}
//private readonly ILogger<HomeController> _logger;
//public HomeController(ILogger<HomeController> logger)
//{
// _logger = logger;
//}
[HttpGet]
public IActionResult Index()
{
CollectionsData model = new CollectionsData();
model.Offers = GetOffers();
model.Services = GetServices();
model.News = GetNews();
model.Team = GetSecurityTeam();
return View(model);
}
[HttpPost]
public IActionResult Contact(ContactUs model)
{
db.Contactus.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
public IActionResult Services( Sevices model)
{
var ModelServices = db.Sevices.ToList();
return View(ModelServices);
}
//[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
//public IActionResult Error()
//{
// return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
//}
public IEnumerable<Offers> GetOffers()
{
return db.Offers.ToList();
}
public IEnumerable<Sevices> GetServices()
{
return db.Sevices.ToList();
}
public IEnumerable<ourNews> GetNews()
{
return db.News.ToList();
}
public IEnumerable<SecurityTeam> GetSecurityTeam()
{
return db.Team.ToList();
}
}
}
But when I delete httppost everything works fine except sending the form to the database
i don't why the page its not load its give HTTP ERROR 405
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在表格的内部放置一个提交按钮
,或者出于某种原因有时会更好
,您需要联系,您需要进行诉讼以创建联系人视图
you have to place a submit button inside of the form
or for some reason sometimes this works better
and you need Contact get action to create a contact view
尝试一下
try this
返回视图()
从HTTP动词的角度看不到任何意义。您的表格发布正确,但是当您返回视图时,您需要将其更改为重定向到您的获取行动。我不知道您的整个控制器是什么样子,但我怀疑这是命名的问题。尝试
返回RedirectToAction(“ viewName”)
return View()
doesn’t make any sense from an HTTP Verb perspective. Your form is POSTing correctly, but when you’re returning the view you need to change it to redirect to your GET action. I don’t know know what your entire controller looks like but I suspect this is the issue with naming.Try
return RedirectToAction(“VIEWNAME”)