重写控制器操作方法的 URL

发布于 2024-10-03 16:08:25 字数 648 浏览 6 评论 0原文

我有一个名为 Person 的控制器,它有一个名为 NameSearch 的 post 方法。

此方法返回 RedirectToAction("Index")、View("SearchResults") 或 View("Details")。 我得到的所有 3 种可能性的网址是 http://mysite.com/Person/NameSearch。 我如何更改此设置以将 URL 重写为 http://mysite.com/Person/Index 以进行 RedirectToAction (“索引”),http://mysite.com/Person/SearchResults 用于查看(“SearchResults” ),以及 http://mysite.com/Person/Details 用于查看(“详细信息”)。

提前致谢

I have a controller called Person and it has a post method called NameSearch.

This method returns RedirectToAction("Index"), or View("SearchResults"), or View("Details").
The url i get for all 3 possibilities are http://mysite.com/Person/NameSearch.
How would i change this to rewrite the urls to http://mysite.com/Person/Index for RedirectToAction("Index"), http://mysite.com/Person/SearchResults for View("SearchResults"), and http://mysite.com/Person/Details for View("Details").

Thanks in advance

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

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

发布评论

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

评论(1

べ映画 2024-10-10 16:08:25

我假设您的 NameSearch 函数评估查询结果并根据以下条件返回这些结果:

  1. 查询是否有效?如果没有,则返回索引。
  2. 结果中是否有 0 或 >1 人,如果是,则发送到搜索结果
  3. 如果结果中恰好有 1 人,则发送到详细信息。

因此,您的控制器或多或少会如下所示:

public class PersonController
{
  public ActionResult NameSearch(string name)
  {
    // Manage query?
    if (string.IsNullOrEmpty(name))
      return RedirectToAction("Index");

    var result = GetResult(name);
    var person = result.SingleOrDefault();
    if (person == null)
      return RedirectToAction("SearchResults", new { name });

    return RedirectToAction("Details", new { id = person.Id });
  }

  public ActionResult SearchResults(string name)
  {
    var model = // Create model...

    return View(model);
  }

  public ActionResult Details(int id)
  {
    var model= // Create model...

    return View(model);
  }
}

因此,您可能需要定义路由,以便:

routes.MapRoute(
  "SearchResults",
  "Person/SearchResults/{name}",
  new { controller = "Person", action = "SearchResults" });

routes.MapRoute(
  "Details",
  "Person/Details/{id}",
  new { controller = "Person", action = "Details" });

Index 操作结果将由默认的 {controller}/{action} 处理/{id} 路线。

这推动你走向正确的方向?

I'm assuming your NameSearch function evaluates the result of a query and returns these results based on:

  1. Is the query valid? If not, return to index.
  2. Is there 0 or >1 persons in the result, if so send to Search Results
  3. If there is exactly 1 person in the result, send to Details.

So, more of less your controller would look like:

public class PersonController
{
  public ActionResult NameSearch(string name)
  {
    // Manage query?
    if (string.IsNullOrEmpty(name))
      return RedirectToAction("Index");

    var result = GetResult(name);
    var person = result.SingleOrDefault();
    if (person == null)
      return RedirectToAction("SearchResults", new { name });

    return RedirectToAction("Details", new { id = person.Id });
  }

  public ActionResult SearchResults(string name)
  {
    var model = // Create model...

    return View(model);
  }

  public ActionResult Details(int id)
  {
    var model= // Create model...

    return View(model);
  }
}

So, you would probably need to define routes such that:

routes.MapRoute(
  "SearchResults",
  "Person/SearchResults/{name}",
  new { controller = "Person", action = "SearchResults" });

routes.MapRoute(
  "Details",
  "Person/Details/{id}",
  new { controller = "Person", action = "Details" });

The Index action result will be handled by the default {controller}/{action}/{id} route.

That push you in the right direction?

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