views:

33

answers:

2

In my global asax file, I want to map a route such as this:

http://domain.com/add/link?url=http%3A%2F%2Fgoogle.com

And then catch it using my LinkController with action called Add.

Do I do this?

global.asax->

routes.MapRoute(
    "AddLink",
    "Add/Link?{url}",
    new { controller = "Link", action = "Add" }
);

LinkController->

public string Add(string url)
{
    return url; // just want to output it to the webpage for testing
}

?? That doesn't seem to work. What am I doing wrong? Thanks!

+5  A: 

ASP.Net MVC will automatically bind parameters from the query string; you don't need to put it in the route.

Your route can simply be

routes.MapRoute(
    "AddLink",
    "Add/Link",
    new { controller = "Link", action = "Add" }
);
SLaks
Thanks so much! That did it.
Ian Davis
A: 

Show mvc source code ValueProviderFactories.

namespace System.Web.Mvc {
    using System;

    public static class ValueProviderFactories {

        private static readonly ValueProviderFactoryCollection _factories = new ValueProviderFactoryCollection() {
            new FormValueProviderFactory(),
            new RouteDataValueProviderFactory(),
            new QueryStringValueProviderFactory(),
            new HttpFileCollectionValueProviderFactory()
        };

        public static ValueProviderFactoryCollection Factories {
            get {
                return _factories;
            }
        }

    }
}
takepara
um, do what now? ;)
Ian Davis
Action parameters binding IModelBinder. DefaultModelBinder extract value from HttpRequest use ValueProviderFactories. So action parameter auto bind Form/Route/QueryString/UplodFile.
takepara