客户端的GET请求路由

Routing on GET request with client side

本文关键字:路由 请求 GET 客户端      更新时间:2023-09-26

与Julian的情况类似:MVC - Route with querystring

我不懂如何操作一个表单,用GET请求,从表单定义路由和值。

(编辑:本质上与朱利安的问题相同的上下文,但在javascript库和/或自定义路由方面询问具体的解决方案(而不是一般领域,并解释为什么有一个问题和不同的方法需要给定的代码);也不使用global.asax;一年多以来的问题意味着其他选项也可能可用)

作为一个初学者,很难在庞大的客户端库中走得更远,也不知道从哪里开始使用相关的自定义路由提供程序,然而,为了保持服务器端路由和301重定向的简单性,这似乎更受欢迎。

尝试了不同的路径(显然不是'custom'),并查看了许多库,但确实没有取得任何实质性的进展。

任何简单的指针,例如路由示例/关键字/链接,简单的客户端代码示例(针对此上下文等)将非常有用。


使用本教程创建电影搜索页面,通过标题类型

下面是我的具体代码:

RouteConfig :

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Movies",
            url: "{controller}/{action}/{title}/{genre}",
            defaults: new 
                {
                    controller = "Home",
                    action = "Index"
                }
        );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new 
                { 
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                }
        );
    }

ActionResult :

public ActionResult SearchIndex(string title, string genre)
{
            var genreQuery = from g in db.Movies
                         orderby g.Genre
                         select g.Genre;
            var genres = new List<string>();
            genres.AddRange(genreQuery.Distinct());
            ViewBag.Genre = new SelectList(genres);
            var movies = from m in db.Movies
                         select m;
            if (!string.IsNullOrEmpty(title))
            {
                movies = movies.Where(s => s.Title.Contains(title));
            }
            if (!string.IsNullOrEmpty(genre))
            {
                movies = movies.Where(s => s.Genre == genre);
            }
            return View(movies);
        }

SearchIndex.cshtml:

    @model IEnumerable<DefaultMvcIA.Models.Movie>
@{
    ViewBag.Title = "SearchIndex";
}
<h2>SearchIndex</h2>
<p>
    @Html.ActionLink("Create New", "Create")
    @using (Html.BeginForm("SearchIndex", "Movies", FormMethod.Get))
    {
        <p>Genre: @Html.DropDownList("Genre", "All")
        Title: @Html.TextBox("Title")<br />
        <input type="submit" value="Filter" /></p>
    }
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Genre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.MovieID }) |
            @Html.ActionLink("Details", "Details", new { id=item.MovieID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.MovieID })
        </td>
    </tr>
}
</table>

在GET请求浏览器只是使用querystring,而不是在routecconfig中设置路由(可以理解)。需要编写自定义路由来将这些查询字符串重定向到路由或使用客户端库。具体信息真的很有用,因为有很多不同的路由库,不知道从哪里开始(首选)301自定义路由方法。

编辑:8:46 PM

控制器/动作,即。Search/Index可以重载以返回不同的结果,这取决于是否有一个querystring和它是否是一个get请求等。

public ActionResult Index(){} will be hit by host/ControllerName/Index
public ActionResult Index(string searchTerm) will be hit by host/ControllerName/Index?searchTerm=ABC

EDIT: 9:23 AM

如果你需要动态改变它所发送的动作,有一些javascript库可以拦截get请求。如果你不介意使用ajax,你可以随时查看http://www.malsup.com/jquery/form/来拦截表单请求,你可以将链接更改为你想要的任何内容。

编辑:6/16 9:22 AM在上面的代码中,有一行

@using (Html.BeginForm("SearchIndex", "Movies", FormMethod.Get))

这表明Get请求将执行一个"Get"请求到"searchchindexcontroller"answers"Movies"ActionResult。

(所以它仍然使用全局中的代码。这个路由代码在global中。Asax 总是用于每个请求。


针对如何访问查询字符串(以重新路由路径)进行编辑

可以直接绑定到querystring。要避免在其他答案上重复文本,请参见ASP。. NET MVC -获取QueryString值用于从查询中获取值的一些方法。

如果你不想在ActionResult方法签名中直接绑定到它们,请使用Request。对象来访问查询字符串。ie .

 var searchTerm = Request.QueryString["searchTerm"].ToString();
 var sortColumn = Request.QueryString["sortColumn"].ToString();

 var searchTerms = Request.QueryString["searchTerms"].ToString().SplitBy(",")

取决于查询字符串的语法…

要根据querystring参数的结果重定向方法,您总是可以返回一个不同的操作方法,或者您可以重定向到action

 ie. return this.Index2( ..param...); 

 RedirectToAction("ActionName", "ControllerName")

…有许多方法可以重定向到您选择的操作。


如何"操作"一个表单对我来说不是很有意义。

在mvc中,一个简单的设置是有一个像 这样的表单
@{using(Html.BeginForm("Index","Home",FormMethod.Post)){
  @Html.LabelFor(m => m.Word)
  @Html.TextBoxFor(m => m.Word, new { @Value = Model.Word})
  <input type="submit" value="Submit">
}

,其中"Index"是操作名称,"Home"是控制器名称。这将被路由到HomeController的Action方法。这个路由路径实际上可以在Global.asax中指定的路由中自定义。在全球。例如,有些代码片段看起来像

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

这会将表单A/B/C的url路由到参数Id=3的controller, ActionResult B

我希望这是足够的信息,以帮助您寻找合适的教程遵循。如果你还是一名学生(或者还有大学邮箱或者有朋友),可以在http://www.pluralsight.com/training上观看视频,他们有很棒的介绍视频,可以帮助你学习mvc3/4基础知识。