URL.Action includes id when constructing URL











up vote
20
down vote

favorite
4












I'm using ASP.Net MVC. Here's my code snippets from a controller named Course:



public ActionResult List(int id)
{
var viewmodel.ShowUrl = Url.Action("Show", "Course");


...
}

public ActionResult Show(int id)
{
...
}


viewmodel.ShowUrl picks up whatever the value is of the "id" parameter. So viewmodel.ShowUrl becomes "/Course/Show/151" (value of id is 151); I want to be able to set the id part on the client based on user interaction. I want the value of viewmodel.ShowUrl to be "/Course/Show".



This seems like a bug to me. I'm not telling Url.Action to include an id value. It's doing it on its own. If I want to set the id value then I would do something like this:



var viewmodel.ShowUrl = Url.Action("Show", "Course", new {id = somevalue});


So, how do you prevent MVC from adding the id value? I can hardcode viewmodel.ShowUrl to "/Course/Show" but that seems to be a kludgy solution. Thanks.










share|improve this question






















  • I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.
    – Bart Calixto
    Aug 12 '13 at 17:15















up vote
20
down vote

favorite
4












I'm using ASP.Net MVC. Here's my code snippets from a controller named Course:



public ActionResult List(int id)
{
var viewmodel.ShowUrl = Url.Action("Show", "Course");


...
}

public ActionResult Show(int id)
{
...
}


viewmodel.ShowUrl picks up whatever the value is of the "id" parameter. So viewmodel.ShowUrl becomes "/Course/Show/151" (value of id is 151); I want to be able to set the id part on the client based on user interaction. I want the value of viewmodel.ShowUrl to be "/Course/Show".



This seems like a bug to me. I'm not telling Url.Action to include an id value. It's doing it on its own. If I want to set the id value then I would do something like this:



var viewmodel.ShowUrl = Url.Action("Show", "Course", new {id = somevalue});


So, how do you prevent MVC from adding the id value? I can hardcode viewmodel.ShowUrl to "/Course/Show" but that seems to be a kludgy solution. Thanks.










share|improve this question






















  • I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.
    – Bart Calixto
    Aug 12 '13 at 17:15













up vote
20
down vote

favorite
4









up vote
20
down vote

favorite
4






4





I'm using ASP.Net MVC. Here's my code snippets from a controller named Course:



public ActionResult List(int id)
{
var viewmodel.ShowUrl = Url.Action("Show", "Course");


...
}

public ActionResult Show(int id)
{
...
}


viewmodel.ShowUrl picks up whatever the value is of the "id" parameter. So viewmodel.ShowUrl becomes "/Course/Show/151" (value of id is 151); I want to be able to set the id part on the client based on user interaction. I want the value of viewmodel.ShowUrl to be "/Course/Show".



This seems like a bug to me. I'm not telling Url.Action to include an id value. It's doing it on its own. If I want to set the id value then I would do something like this:



var viewmodel.ShowUrl = Url.Action("Show", "Course", new {id = somevalue});


So, how do you prevent MVC from adding the id value? I can hardcode viewmodel.ShowUrl to "/Course/Show" but that seems to be a kludgy solution. Thanks.










share|improve this question













I'm using ASP.Net MVC. Here's my code snippets from a controller named Course:



public ActionResult List(int id)
{
var viewmodel.ShowUrl = Url.Action("Show", "Course");


...
}

public ActionResult Show(int id)
{
...
}


viewmodel.ShowUrl picks up whatever the value is of the "id" parameter. So viewmodel.ShowUrl becomes "/Course/Show/151" (value of id is 151); I want to be able to set the id part on the client based on user interaction. I want the value of viewmodel.ShowUrl to be "/Course/Show".



This seems like a bug to me. I'm not telling Url.Action to include an id value. It's doing it on its own. If I want to set the id value then I would do something like this:



var viewmodel.ShowUrl = Url.Action("Show", "Course", new {id = somevalue});


So, how do you prevent MVC from adding the id value? I can hardcode viewmodel.ShowUrl to "/Course/Show" but that seems to be a kludgy solution. Thanks.







asp.net-mvc-3 asp.net-mvc-routing url.action






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 17 '12 at 22:23









Tom Schreck

1,7051047101




1,7051047101












  • I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.
    – Bart Calixto
    Aug 12 '13 at 17:15


















  • I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.
    – Bart Calixto
    Aug 12 '13 at 17:15
















I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.
– Bart Calixto
Aug 12 '13 at 17:15




I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.
– Bart Calixto
Aug 12 '13 at 17:15












3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.



routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
);


Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.



On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.



public ActionResult Show(int? id)
public ActionResult Show(int id = 0)


Otherwise you'll get an error when you try loading the url without the id parameter.






share|improve this answer




























    up vote
    23
    down vote













    Just came across the same problem and so you know, you can also just use an empty string:



    @Url.Action("Show", "Course", new { id = "" })





    share|improve this answer





















    • Thanks @Ben. This is the only one that worked for me.
      – JRodd
      Aug 31 at 19:45


















    up vote
    11
    down vote













    I know this is old, but I found this first, but didn't like any of these solutions, so I kept looking and found https://stackoverflow.com/a/19110921/1130636.



    You can use UrlParameter.Optional to solve this problem



    Url.Action("Show", "Course", new { id = UrlParameter.Optional })






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10200078%2furl-action-includes-id-when-constructing-url%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      7
      down vote



      accepted










      I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.



      routes.MapRoute(
      "Default", // Route name
      "{controller}/{action}/{id}", // URL with parameters
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
      );


      Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.



      On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.



      public ActionResult Show(int? id)
      public ActionResult Show(int id = 0)


      Otherwise you'll get an error when you try loading the url without the id parameter.






      share|improve this answer

























        up vote
        7
        down vote



        accepted










        I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.



        routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
        );


        Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.



        On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.



        public ActionResult Show(int? id)
        public ActionResult Show(int id = 0)


        Otherwise you'll get an error when you try loading the url without the id parameter.






        share|improve this answer























          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.



          routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
          new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
          );


          Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.



          On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.



          public ActionResult Show(int? id)
          public ActionResult Show(int id = 0)


          Otherwise you'll get an error when you try loading the url without the id parameter.






          share|improve this answer












          I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.



          routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
          new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
          );


          Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.



          On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.



          public ActionResult Show(int? id)
          public ActionResult Show(int id = 0)


          Otherwise you'll get an error when you try loading the url without the id parameter.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 18 '12 at 0:07









          DMulligan

          6,16532331




          6,16532331
























              up vote
              23
              down vote













              Just came across the same problem and so you know, you can also just use an empty string:



              @Url.Action("Show", "Course", new { id = "" })





              share|improve this answer





















              • Thanks @Ben. This is the only one that worked for me.
                – JRodd
                Aug 31 at 19:45















              up vote
              23
              down vote













              Just came across the same problem and so you know, you can also just use an empty string:



              @Url.Action("Show", "Course", new { id = "" })





              share|improve this answer





















              • Thanks @Ben. This is the only one that worked for me.
                – JRodd
                Aug 31 at 19:45













              up vote
              23
              down vote










              up vote
              23
              down vote









              Just came across the same problem and so you know, you can also just use an empty string:



              @Url.Action("Show", "Course", new { id = "" })





              share|improve this answer












              Just came across the same problem and so you know, you can also just use an empty string:



              @Url.Action("Show", "Course", new { id = "" })






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 9 '14 at 6:59









              Ben Cull

              7,82653235




              7,82653235












              • Thanks @Ben. This is the only one that worked for me.
                – JRodd
                Aug 31 at 19:45


















              • Thanks @Ben. This is the only one that worked for me.
                – JRodd
                Aug 31 at 19:45
















              Thanks @Ben. This is the only one that worked for me.
              – JRodd
              Aug 31 at 19:45




              Thanks @Ben. This is the only one that worked for me.
              – JRodd
              Aug 31 at 19:45










              up vote
              11
              down vote













              I know this is old, but I found this first, but didn't like any of these solutions, so I kept looking and found https://stackoverflow.com/a/19110921/1130636.



              You can use UrlParameter.Optional to solve this problem



              Url.Action("Show", "Course", new { id = UrlParameter.Optional })






              share|improve this answer



























                up vote
                11
                down vote













                I know this is old, but I found this first, but didn't like any of these solutions, so I kept looking and found https://stackoverflow.com/a/19110921/1130636.



                You can use UrlParameter.Optional to solve this problem



                Url.Action("Show", "Course", new { id = UrlParameter.Optional })






                share|improve this answer

























                  up vote
                  11
                  down vote










                  up vote
                  11
                  down vote









                  I know this is old, but I found this first, but didn't like any of these solutions, so I kept looking and found https://stackoverflow.com/a/19110921/1130636.



                  You can use UrlParameter.Optional to solve this problem



                  Url.Action("Show", "Course", new { id = UrlParameter.Optional })






                  share|improve this answer














                  I know this is old, but I found this first, but didn't like any of these solutions, so I kept looking and found https://stackoverflow.com/a/19110921/1130636.



                  You can use UrlParameter.Optional to solve this problem



                  Url.Action("Show", "Course", new { id = UrlParameter.Optional })







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 23 '17 at 12:17









                  Community

                  11




                  11










                  answered Mar 13 '15 at 17:01









                  borigas

                  32737




                  32737






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10200078%2furl-action-includes-id-when-constructing-url%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Sphinx de Gizeh

                      Dijon

                      Équipe cycliste