删除操作不起作用 MVC 3

发布于 2025-01-06 23:16:10 字数 988 浏览 0 评论 0原文

我定义的路线是:

    map.Route(new Route("Cars/{id}/Delete",  
              new RouteValueDictionary(new { controller = "Car", action = "Delete"}),
              new MvcRouteHandler()));

在我看来,我得到了:

<a href="/Car/@Model.Id/Delete">Delete</a>

运行时尝试将请求发送到 http:// oursite/Car/122/Delete

我在这个汽车控制器中的删除操作如下所示:

public ActionResult Delete(int id)
{
    //code is here
}

我注意到了几件事:

  1. 如果我通过我的 PC 在本地运行相同的代码,删除操作会完美无缺< /strong> 并且能够到达我的行动方法。我在 IIS 7 / Win 7 上运行此代码

  2. 在我们的开发服务器上,它显然是通过 IIS7 设置的,但该路由失败并表示无法在路由表上找到该路由。但这与我在本地使用的相同路由表类...所以为什么我会得到这个:

路由表中没有路由与提供的值匹配。

但为什么这在开发服务器上不起作用呢?当我将本地设置与服务器的设置进行比较时,我发现 IIS 中的设置大部分相同。

我注意到,无论是本地主机还是服务器,如果我尝试在删除操作上添加 [HttpDelete] 属性,它找不到我的操作方法,并且收到一条错误消息,指出找不到该方法。所以不知道为什么当我把它取下来时,删除有效(仅限本地主机)

Route I have defined is:

    map.Route(new Route("Cars/{id}/Delete",  
              new RouteValueDictionary(new { controller = "Car", action = "Delete"}),
              new MvcRouteHandler()));

In my view I've got:

<a href="/Car/@Model.Id/Delete">Delete</a>

Which when run tries to send a request to http://oursite/Car/122/Delete

My delete action in this Car controller looks like this:

public ActionResult Delete(int id)
{
    //code is here
}

I noticed a couple things:

  1. If I run this same code locally via my PC, the delete works flawlessly and is able to get to my action method. I'm running this over IIS 7 / Win 7

  2. On our dev server, it's setup obviously via IIS7 but this route fails and says it can't find the route on our route table. But this is the SAME route table class I am using locally...so why would I get this:

No route in the route table matches the supplied values.

But why would that not work on a dev server? I see the setup identical in IIS for the most part as far as I can see when I compare my local setup to the server's.

I noticed that also whether localhost or server, if I try and put an [HttpDelete] attribute on my delete action, it doesn't find my action method and I get an error saying it can't find that method. So not sure why when I take that off, the delete works (localhost only)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

痕至 2025-01-13 23:16:10

使用助手生成链接:

@Html.ActionLink("Delete", "Delete", "Car");

第一个参数是链接文本,第二个参数是操作方法名称,第三个参数是控制器名称。

请参阅有关 ActionLink() 的此 MSDN 参考

Use a helper to generate your link:

@Html.ActionLink("Delete", "Delete", "Car");

The first parameter is your link text, the second is your Action method name, and the third is your Controller name.

See this MSDN Reference on ActionLink().

土豪 2025-01-13 23:16:10

您能分享一下视图的代码吗?如何在视图中构建“a”标签?

关于[HttpDelete]属性,这意味着该方法需要HTTP“DELETE”请求。 “a”标签始终有 GET 请求。
请参阅此链接

Could you please share code for the View. How do you build the 'a' tag in the view?

Regarding the [HttpDelete] attribute, it means that the method needs the HTTP 'DELETE' request. The 'a' tag always has a GET request.
Please refer this link

巷子口的你 2025-01-13 23:16:10

我想你回答了你自己的问题。路由表中没有与您提供的值匹配的路由。您可以通过在 Global.asax.cs 文件中写入以下内容来编写该路线:

public class Global : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    // Specify routes
    RouteTable.Routes.Add(new Route
    {
      Url = "[controller]/[id]/[action]",
      Default = new { controller = "Car" },
      RouterHandler = typeof(MvcRouteHandler)
    });
  }
}

或者,您可以使用现有路线(我个人的建议)来使用汽车控制器中的删除功能。为此,请尝试将代码切换为:

<a href="/Car/Delete/@Model.Id">Delete</a>

I think you answered your own question. There is no route in the route table that matches your supplied values. You could write that route to do that by writing this in your Global.asax.cs file:

public class Global : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    // Specify routes
    RouteTable.Routes.Add(new Route
    {
      Url = "[controller]/[id]/[action]",
      Default = new { controller = "Car" },
      RouterHandler = typeof(MvcRouteHandler)
    });
  }
}

Or, you can use existing routes (my personal recommendation) to use the Delete function in your Car controller. To do that, try switching your code to this:

<a href="/Car/Delete/@Model.Id">Delete</a>
橘和柠 2025-01-13 23:16:10

首先命名该路线

map.Route("DeleteCar",new Route("Cars/{id}/Delete",  
          new RouteValueDictionary(new { controller = "Car", action = "Delete"}),
          new MvcRouteHandler()));

然后

<a href="@Url.RouteUrl("DeleteCar",new{id=Model.Id})">Delete</a>

除非该链接转到警告屏幕,否则我强烈建议删除应该是 POST 甚至 DELETE(我认为可以通过 ajax 设置)

First name that route

map.Route("DeleteCar",new Route("Cars/{id}/Delete",  
          new RouteValueDictionary(new { controller = "Car", action = "Delete"}),
          new MvcRouteHandler()));

Then

<a href="@Url.RouteUrl("DeleteCar",new{id=Model.Id})">Delete</a>

Unless that link goes to a warning screen, I strongly suggest that a delete should be a POST or even a DELETE(I think it can be set via ajax)

昔梦 2025-01-13 23:16:10

本地主机和我们网站之间的 URL 路径可能存在差异。路径“/Car/@Model.Id/Delete”是硬编码的,未解析,并且可能无法在所有环境中工作。正如其他答案中所建议的,使用 MVC 帮助程序(如 @Html.ActionLink 或 @Url.RouteUrl)来解析本地环境的路径。

There's likely a difference in the URL paths between localhost and oursite. The path "/Car/@Model.Id/Delete" is hard-coded, not resolved and may not work in all environments. As suggested in other answers, use an MVC helper like @Html.ActionLink or @Url.RouteUrl to resolve the path for the local environment.

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