【问题标题】:I can not configure the route我无法配置路由
【发布时间】:2019-08-17 10:31:18
【问题描述】:

我用一个控制器创建了一个简单的搜索引擎,该控制器有一个可以接受学生参数的方法

这是我的剃须刀页面代码

<form method="post">
    <div class="row form-group text-center">
        <div class="col-md-3">
            <input placeholder="Город" class="form-control" asp-for="@Model.Profession" />
        </div>
        <div class="col-md-2">
            <input placeholder="Город" class="form-control" asp-for="@Model.City" />
        </div>
        <div class="col-md-2">
            <input placeholder="Курс" class="form-control mdl-textfield__input" asp-for="@Model.Course" />
        </div>
        <div class="col-md-2">
            <input placeholder="Специализация" class="form-control" asp-for="@Model.Specialization" />
        </div>
        <div class="col-md-2">
            <input type="submit" value="Поиск" class="btn btn-primary " />
        </div>
    </div>
</form>

这是用户模型

public List<Student> Students { get; set; }
public string Profession { get; set; }
public string City { get; set; }
public int Course { get; set; }
public string Specialization { get; set; }

我的控制器 GET 和 POST

 [Route("Index")]
 [HttpGet]
 public IActionResult Index()

 [Route("Index")]
 [HttpPost]
 public IActionResult Index(UserModel model)

在没有参数的情况下搜索我的溃败搜索/索引。如何创建像 Search/Index/Profession=Coder/City=London/Course=4/Specialization=Code 这样的溃败。我的路线是静态的,我无法返回或复制 url。但我发现文档已过期

对不起,我的英语不好。

【问题讨论】:

    标签: asp.net-mvc routes


    【解决方案1】:

    下面是一个基本的例子

    [RoutePrefix("Search")]               //place this routeprefix since "Search' is common
    public class TestController: Controller
    {
        [Route("Index")]                 // "Search/Index" route for GET 
        [HttpGet]
        public IActionResult Index()
    
        [Route("Index")]                  // "Search/Index" route for POST 
        [HttpPost]
        public IActionResult Index(UserModel model)
    

    现在为Search/Index/Profession=Coder/City=London/Course=4/Specialization=Code。 首先,根据我的理解,它应该如下所示。

    请仔细检查。

    Search/Index?Profession=Coder&amp;City=London&amp;Course=4&amp;Specialization=Code

    关于上述路线的说明。在Index 之后,它包含?,这表明查询字符串跟在? 之后。同样Profession=Coder是一对查询字符串,后面跟着&amp;,表示有另一个查询字符串City=London,后面跟着&amp;,表示还有另一个查询字符串Course=4,以后再说。

    定义的路由如下

        [Route("Index/{Profession=Profession}/{City=City}/{Course=Course}/{Specialization=Specialization}")]  
        [HttpGet]
        public IActionResult Profession(string Profession,string City,int Course,string Specialization)
    

    【讨论】:

    • [RoutePrefix("Search")] 找不到。如何解决?
    • RoutePrefix 在 System.Web.Mvc asp.net mvc5 的命名空间中
    • 不需要使用双“索引”属性。如果方法标记为 get 或 post 相同的命名操作将被处理请求。
    • 我用的是2.2版本
    • asp net core mvc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2012-06-21
    相关资源
    最近更新 更多