0

I have read here : Routing with Multiple Parameters using ASP.NET MVC. But still not worked in my case.

I have EmitenController which there a function like this:

public async Task<ActionResult> Financial(string q = null, int page = 1)

At first load, the URL that produced by this function: Emiten/Financial?q=&page=1. The next page of course Emiten/Financial?q=&page=2.

If I give the query that URL become: Emiten/Financial?q=query&page=1 and go on.

For the routes, I have tried

routes.MapRoute(
   name: "Financial",
   url: "{controller}/{action}/{q}/{page}",
   defaults: new { controller = "Emiten", action = "Financial", q = "", page = 1 }
);

But, when I try go to page 3 the URL still Emiten/Financial?q=&page=2 and how about the URL if I give q empty value?

Thanks in advance.

Community
  • 1
  • 1
andrefadila
  • 647
  • 2
  • 9
  • 36

2 Answers2

0

If the route is for a specific action method, then you should specify the action method directly in the URL settings

routes.MapRoute(
   name: "Financial",
   url: "Emiten/Financial/{q}/{page}", // <---- Right here
   defaults: new 
   { 
       controller = "Emiten", 
       action = "Financial", 
       q = string.Empty, // string.Empty is the same as ""
       page = 1,
   }
);

In your action method, you don't need to specify the default values since you already did that in your route config.

public async Task<ActionResult> Financial(string q, int page)

Let me know how it works for you

UPDATE:

Generating a link relative to a route

@Html.RouteLink(
    linkText: "Next Page", 
    routeName: "Financial", 
    routeValues: new { controller = "Emiten", action = "Financial", q = ViewBag.SearchKey, page = nextPage})

Related Link: What's the difference between RouteLink and ActionLink in ASP.NET MVC?

Community
  • 1
  • 1
Yorro
  • 11,445
  • 4
  • 37
  • 47
  • 1
    The constraints seem a little redundant as the method will throw an exception that you passed it an invalid value, which might be more useful then getting a 404 for not matching any routes. – siva.k Aug 20 '14 at 03:09
  • @siva.k - Thanks for that, I removed it. – Yorro Aug 20 '14 at 03:10
  • @andrefadila Did you try to manually type the url? Can you show us the method you used to generate the url? – Yorro Aug 20 '14 at 05:12
  • Oh I see, I am using this `@Url.Action("Financial", "Emiten", new { q = ViewBag.SearchKey, page = nextPage })` (nextPage is number) for href attribute on link next page. What should I use? – andrefadila Aug 20 '14 at 06:15
  • @andrefadila - I have updated my answer to show how to generate a route link. – Yorro Aug 20 '14 at 08:01
0

I can't seem to reproduce your issue. Are you sure you mapped your Financial route before your Default one?

Here is my RegisterRoutes method:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Financial",
        "{controller}/{action}/{q}/{page}",
        new { controller = "Emiten", action = "Financial", q = string.Empty, page = 1 }
    );

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

Here is the Controller:

public class EmitenController : Controller
{
    public async Task<ActionResult> Financial(string q, int page)
    {
        return View();
    }
}

(I know the async is useless in this case, but you can't await a ViewResult) And here is the View:

q = @Request.QueryString["q"]<br/>
page = @Request.QueryString["page"]

@Html.ActionLink("Page 2", "Financial", "Emiten", new { q = "test", page = 2 }, null)
<br/>
<a href="@Url.Action("Financial", "Emiten", new { q = "test", page = 3 })">Page 3</a>

Everything works as expected

Marien Monnier
  • 205
  • 2
  • 10