HTTPPOST返回页面找不到HTTP错误405 ASP.NET Core MVC IIS Express

发布于 2025-02-08 07:58:29 字数 2665 浏览 2 评论 0原文

当我放入控制器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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏辞 2025-02-15 07:58:29

您必须在表格的内部放置一个提交按钮

<form asp-controller="Home" asp-action="Contact" method="POST">
    ....your code

    <button type="submit">Contact Us</button>
</form>

,或者出于某种原因有时会更好

@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
   ....your code

    <button type="submit">Contact Us</button>
}

,您需要联系,您需要进行诉讼以创建联系人视图

        [HttpGet]
        public IActionResult Contact()
        {
            var model= new ContactUs();
            return View(model)
        }

you have to place a submit button inside of the form

<form asp-controller="Home" asp-action="Contact" method="POST">
    ....your code

    <button type="submit">Contact Us</button>
</form>

or for some reason sometimes this works better

@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
   ....your code

    <button type="submit">Contact Us</button>
}

and you need Contact get action to create a contact view

        [HttpGet]
        public IActionResult Contact()
        {
            var model= new ContactUs();
            return View(model)
        }
浅语花开 2025-02-15 07:58:29

尝试一下

<form id="contact"  method="POST" asp-action="Contact"> 
  ...     
</form>

try this

<form id="contact"  method="POST" asp-action="Contact"> 
  ...     
</form>
寄居人 2025-02-15 07:58:29

返回视图()从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”)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文