开发者

Action name containing a question mark?

Using asp.net mvc 3.0 what would i have to do to provide the following route

public class ProductController : Controller
{
    // ************************
    // URL : Product/Create
    // ************************

    pu开发者_开发知识库blic ActionResult Create()
    {
       return View();
    }

    // ************************
    // URL : Product/Create?Page=Details
    // ************************

    [ActionName("Create?Page=Details")]
    public ActionResult CreateDetails()
    {
       return View();
    }
}

Thanks

Rohan


public class QueryStringConstraint : IRouteConstraint
{
    public QueryStringConstraint(string value, bool ignoreCase = true)
    {
        Value = value;
        IgnoreCase = ignoreCase;
    }

    public string Value { get; private set; }
    public bool IgnoreCase { get; private set; }

    public virtual bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var currentValue = httpContext.Request.QueryString[parameterName];

        return IgnoreCase ? currentValue.ToLowerInvariant() == Value.ToLowerInvariant() : currentValue == Value;
    }
}

routes.MapRoute("Create page details", "Product/Create", 
    new { controller = "Product", action = "CreateDetails" }, 
    new { page = new QueryStringConstraint("details") });

Alternatively if you have different models for those actions, you could do something like this (with standard "{controller}/{action}/{optional id}" route):

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute
{
    public RequireRequestValueAttribute(string name, string value = null, bool ignoreCase = true)
    {
        Name = name;
        Value = value;
        IgnoreCase = ignoreCase;
    }

    public string Name { get; private set; }
    public string Value { get; private set; }
    public bool IgnoreCase { get; private set; }

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        var value = controllerContext.HttpContext.Request[Name];

        return value != null && (Value == null || (IgnoreCase ? Value.ToLowerInvariant() == value.ToLowerInvariant() : Value == value));
    }
}

[RequireRequestValue("Page", "Detail")]
public ActionResult Create(ProductDetailModel model)
{
    return View(model);
}
[RequireRequestValue("Page", "Overview")]
public ActionResult Create(ProductOverviewModel model)
{
    return View(model);
}


An action name cannot contain a question mark. The question mark is a reserved character in a URL indicating the beginning of the query string.


What if you not create another action? Just call your "Create" action with the query string.

http://localhost/Home/Create?Page=Details

public ActionResult Create()
{
    var page = Request.QueryString["Page"];

    // do your stuff, or redirect here if you like
    // return RedirectToAction("Create" + page);

    return View();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜