开发者

How to route a query string with "?" and how to handle it

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!


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" }
);


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;
            }
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜