1

I have an action link in HTML.

<a href="@Url.Action("UserWisedPost", "Blog", new { userName = item.UserName,userId=item.UserId })">@item.UserName</a>

In controller,

public ActionResult UserWisedPost(string userId,string userName, int? page, int pageSize = 10)
{
     page = page == null || page == 0 ? 1 : page;
     var model = _post.GetByUserId(userId).ToPagedList((int)page, pageSize); 
     return View(model);
}

And in RouteConfig

routes.MapRoute(
                name: "userwisedPost",
                url: "{controller}/{action}/{userName}",
                defaults: new { controller = "Blog", action = "UserWisedPost", userName=UrlParameter.Optional}
            );

You have probably understood what I want to mean. I want to display URL like ../UserWisedPost/userName but want access data by userId in controller.

How can I do that?

SynozeN Technologies
  • 1,337
  • 1
  • 14
  • 19
orbit
  • 1,228
  • 2
  • 13
  • 23
  • So right now you have something like this: `../UserWisedPost/userName?userId=123` right? If so, what's the issue? – Mihail Stancescu May 23 '17 at 06:16
  • Your controller action should only take the `userName` as input, and then in the action method, you need a way to convert this to a `userId`. Alternatively, you could alter your `_post` to be able to fetch posts by `userName`. But we don't know how your backend is structured, so no one can give an exact answer to your issue. – Lars Kristensen May 23 '17 at 06:22
  • I don't want to show userid. But in controller , I want to access data by userid – orbit May 23 '17 at 06:24

2 Answers2

1

I think the problem here is that the default route is taking precedence.

I'm guessing you have this before your custom route:

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

You need your custom route before this one, otherwise this will be the route that handles requests to your BlogController.

A simple way to solve this would be something like this:

routes.MapRoute(
            name: "userwisedPost",
            url: "Blog/{action}/{userName}",
            defaults: new { controller = "Blog", action = "UserWisedPost", userName = UrlParameter.Optional }
        );

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

This would result in a url like this: Blog/UserWisedPost/SomeUserName?userId=someUserId

Robban
  • 6,729
  • 2
  • 39
  • 47
  • I don't want to show userid in url. But in controller , I want to access data by userid – orbit May 23 '17 at 06:27
  • You need to show the userId if you're passing it as a url-parameter. – Robban May 23 '17 at 06:29
  • The only other way would be to use a form and POST the values. Then they wouldn't show up, but it wouldn't be an anchor, but a form to submit. – Robban May 23 '17 at 06:30
  • See this answer as well: https://stackoverflow.com/questions/8648869/passing-parameters-to-controller-but-not-on-the-url – Robban May 23 '17 at 06:35
1

If you want to have the userId other than query string you should add a form and put it as a hidden field.

Or, you can use AJAX to post the data:

<a href="postUserWised(@Url.Action("UserWisedPost", "Blog"), @item.UserName,@item.UserId)">@item.UserName</a>

Then in your scripts section in that view:

@section scripts {
<script type="text/javascript">
   function postUserWised( url, userName, userId) {
      $.ajax({
        url: url,
        type: POST,
        data: { userName: userName, userId: userId },
        success: function(data) { },
        error: function(...) { } 
      });
   }
</script>
}

I haven't tested this but it should point you in the right direction.

Mihail Stancescu
  • 4,088
  • 1
  • 16
  • 21