Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 操作路由可选id参数_C#_Asp.net_Asp.net Mvc_Routes - Fatal编程技术网

C# 操作路由可选id参数

C# 操作路由可选id参数,c#,asp.net,asp.net-mvc,routes,C#,Asp.net,Asp.net Mvc,Routes,我正在尝试使用可选的/id参数创建控制器操作,如下所示: 默认值http://localhost/tasks idhttp://localhost/tasks/42 以下是控制器和操作: 我尝试在动作中使用路由属性 public class TasksController : AsyncController { [Route("tasks/{id}")] public ActionResult Index(string id) { ... } }

我正在尝试使用可选的/id参数创建控制器操作,如下所示:

  • 默认值<代码>http://localhost/tasks
  • id
    http://localhost/tasks/42
  • 以下是控制器和操作: 我尝试在动作中使用路由属性

    public class TasksController : AsyncController
    {
        [Route("tasks/{id}")]
        public ActionResult Index(string id)
        {
            ...
        }
    }
    
    这只在url中设置了id参数时起作用,但是默认页面
    /tasks
    会抛出未找到的错误,或者,当使用
    [Route(“tasks”)]
    而没有id时,默认页面会起作用,但当设置了id时,会再次抛出未找到的错误。我还尝试了
    [Route(“tasks/{id?}”)]
    将id标记为可选参数,但没有成功


    有没有办法让它工作?

    您必须检查控制器中的参数值:

    public class PlansController : AsyncController
    {
        [Route("tasks/{id}")]
        public ActionResult Index(string id)
        {
            if(id == null)
            {
                 return View("ViewName");
            }
            else
            {
                 //Query DB and get model for the view
                 return View("ViewName", Model);
            }
        }
    }
    

    由于字符串在默认情况下是可为null的,所以您不必担心用户传递null并出现错误的黄色屏幕。

    您可以通过在route参数中添加问号使URI参数成为可选参数。如果路由参数是可选的,则必须为方法参数定义默认值

    在操作方法上设置默认值,并在路由参数末尾添加问号

    [Route("tasks/{id?}")]
    public ActionResult Index(string id =null)
    

    我只是测试了一下,效果很好

    @JohnSaunders,但在搜索与asp.net相关的问题时,大多数问题的标题中都有asp.net MVC,这无疑使发现和查找更容易。我看不出有什么问题,我不知道你在找什么。我只是搜索了一下,事实并非如此。我强烈建议你检查我包含的链接。这些标签已经包含了这些信息,所以你不需要在你的标题中浪费空间,这些空间可以用来让人们更好地了解你的问题所在。@JohnSaunders我指的是谷歌搜索,这一点更为明显。我也检查了链接,我明白了。我不认为这会有帮助,我的代码中已经有了这一部分,但问题是路线这是怎么被否决的?与accepted的唯一区别是id从一开始就被设置为null?我没有否决投票,所以我不知道,但正如我所说的,这种方法的问题是路由
    [route(“tasks/{id}”)]
    ,它需要什么?要成为可选参数,如果您正确设置了路由配置,则可以将该参数设置为可选参数,而不考虑控制器的routes.maprote(名称:“Default”,url:{controller}/{action}/{id}),默认值:new{controller=“Home”,action=“Index”,id=UrlParameter.optional});'Idk如何编辑注释但是可选的id值是在route Config中设置的这似乎很好,我已经尝试了类似的方法,但是在?之前我有一个空格?。当ajax调用此控制器自定义操作时,我的代码还有其他问题,似乎也调用了索引操作。但这可能与此无关,对吗?不管怎样,我认为这是最好的答案好的答案(+1),但我遇到了一个问题:哪一个你不明白
    ContentResult
    {id=demo}
    {id?}
    ContentResult
    ,我知道
    id?
    是一个可选参数,
    id=demo
    可能与
    if(id==“demo”){}相同
    
    public class TestController : AsyncController
    {
        [Route("tasks/{id?}")]
        public ActionResult Index(string id)
        {
            return new ContentResult() { Content = id ?? "Empty" };
        }
    
        [Route("task/{id=demo}")]
        public ActionResult Task(string id)
        {
            return new ContentResult() { Content = id ?? "DemoEmpty" };
        }
    }