[c#]Url 似乎重复

标签: Asp.net HTML Razor C#
发布时间: 2017/2/25 3:44:22
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我使用 asp.net Razor引擎。 我有一个删除按钮,第一次我按下它,它的工作。 第二次的 url 重复作用和不起作用。

这是首次使用删除按钮This is the first time I use the Delete button

enter image description here

这是我使用删除第二次。请注意 URL 是德尔/德尔。 试图避免那第二个 del。

这里是我的控制器

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using DapperApp.Factory;
using login.Models;
using login.Controllers;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;


namespace login.Controllers
{
    public class HomeController : Controller
    {

        private readonly UserFactory userFactory;

        public HomeController(UserFactory user) {
            userFactory = user;
        }

        // GET: /Home/
        [HttpGet]
        [Route("")]
        public IActionResult Index()
        {

            return View();
        }

        [HttpPost]
        [Route("")]
        public IActionResult Register(Home model)
        {
            if(!ModelState.IsValid)
            {
                return View("Index", model);
            }
            PasswordHasher<Home> Hasher = new PasswordHasher<Home>();
            model.Password = Hasher.HashPassword(model, model.Password);
            userFactory.Add(model);
            TempData["message"] = false;

            return RedirectToAction("Index");
        }

        [HttpPost]
        [Route("login")]
        public IActionResult Login(Home model)
        {     
            if(model.Password == null || model.Email == null){
                TempData["login"] = false;
                return RedirectToAction("Index");
            }      
            var pass = userFactory.FindByEmail(model);
            var Hasher = new PasswordHasher<Home>();
            if(pass == null)
            {
                TempData["login"] = false;
                return RedirectToAction("Index");
            }
                // Pass the user object, the hashed password, and the PasswordToCheck
            if(0 != Hasher.VerifyHashedPassword(model, pass.Password, model.Password))
            {
                TempData["first_name"] = pass.First_Name;
                TempData["last_name"] = pass.Last_Name;
                TempData["id"] = pass.Id;
                HttpContext.Session.SetString("Id", pass.Id.ToString());

                ViewBag.Quotes = userFactory.FindAll();
                return View();   
            }
            TempData["login"] = false;

            return RedirectToAction("Index");
        }

        [HttpPost]
        [Route("addQuote")]
        public IActionResult AddQuote(Quotes model)
        {   
            var test = HttpContext.Session.GetString("Id");
            if(!ModelState.IsValid)
            {
                TempData["id"] = test;
                model.Users_id = Convert.ToInt32(test.ToString());
                var user2 = userFactory.FindById(model.Users_id);
                TempData["first_name"] = user2.First_Name;
                TempData["last_name"] = user2.Last_Name;
                ViewBag.Quotes= userFactory.FindAll();
                return View("Login", model);
            }
            if(test == null){
                return RedirectToAction("Index");
            }
            model.Users_id = Convert.ToInt32(test.ToString());
            userFactory.addQuote(model);
            var user = userFactory.FindById(model.Users_id);
            TempData["id"] = test;
            TempData["first_name"] = user.First_Name;
            TempData["last_name"] = user.Last_Name;
            ViewBag.Quotes = userFactory.FindAll();
            return View("Login", model);  
        }
        [HttpGet]
         [Route("logout")]
        public IActionResult Logout()
        { 
            return RedirectToAction("Index");
        }

        [HttpGet]
        [Route("del/{id}")]
        public IActionResult Del(int Id) // This is my delete method
        {
            userFactory.DeleteByID(Id);
             ViewBag.Quotes2= userFactory.FindAll();
             var test = HttpContext.Session.GetString("Id");
             var user = userFactory.FindById(Convert.ToInt32(test));
             TempData["first_name"] = user.First_Name;
            TempData["last_name"] = user.Last_Name;
            TempData["id"] = test;


            return View("Login");
        }   
    }
}

这里是我的 cshtml 页

<h1>Hello @TempData["first_name"] @TempData["last_name"]</h1>
 @if(TempData["first_name"]!=  null)
 {

 }

 @model login.Models.Quotes
<h1>Add Your Quote</h1>
@using(Html.BeginForm("AddQuote","Home"))
{
    <p>
        <label>Your Quote</label>
        @Html.TextAreaFor(d=>d.quotes)
        @Html.ValidationMessageFor(d => d.quotes)
    </p>
    <input type="submit" name="submit" value="Add my quote!"/>
}

<form action="logout" method="get">
    <input type="submit" name="submit" value="Log Out"/>
</form>

<div >
    @{
        if(ViewBag.Quotes != null)
        {
            foreach(var quotes in ViewBag.Quotes)
            {
                //If there are any errors for a field...

               <p><q>@quotes.quotes</q></p> 
               <p class="wrapper">-@quotes.First_Name @quotes.Last_Name at @quotes.Created_At.ToString("hh"):@quotes.Created_At.ToString("mm")
                     @quotes.Created_At.ToString("tt") @quotes.Created_At.ToString("MMM")  @quotes.Created_At.ToString("dd")
                     @quotes.Created_At.ToString("yyyy")</p>
                if(@quotes.Users_id == Convert.ToInt32(TempData["id"].ToString()))
                {
                    <form action="del/@quotes.Id_Quotes" method="get">
                        <input type="submit" name="submit" value="Delete"/>
                    </form> 
                }

            }
        }
        if(ViewBag.Quotes2 != null)
        {
            foreach(var quotes in ViewBag.Quotes2)
            {
                //If there are any errors for a field...

               <p><q>@quotes.quotes</q></p> 
               <p class="wrapper">-@quotes.First_Name @quotes.Last_Name at @quotes.Created_At.ToString("hh"):@quotes.Created_At.ToString("mm")
                     @quotes.Created_At.ToString("tt") @quotes.Created_At.ToString("MMM")  @quotes.Created_At.ToString("dd")
                     @quotes.Created_At.ToString("yyyy")</p>
                if(@quotes.Users_id == Convert.ToInt32(TempData["id"].ToString()))
                {
                    <form action="del/@quotes.Id_Quotes" method="get">
                        <input type="submit" name="submit" value="Delete"/>
                    </form> 
                }

            }
        }

    }
</div>

解决方法 1:

您的窗体操作使用相对 (不是一个具体) 的引用,意味着它将追加行动到当前 url 的末尾每次提交表单。试着把行动你 get 请求的绝对 url。在这种情况下,这就意味着︰

<form action="del/@quotes.Id_Quotes" method="get">
    <input type="submit" name="submit" value="Delete"/>
</form>

变得

<form action="/del/@quotes.Id_Quotes" method="get">
    <input type="submit" name="submit" value="Delete"/>
</form> 

此外,只是要挑剔,当删除的东西 (或添加和编辑的东西) 到数据库应该使用 POST 请求。他们是好得多。

官方微信
官方QQ群
31647020