Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 包含问号的动作名称?_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 包含问号的动作名称?

Asp.net mvc 3 包含问号的动作名称?,asp.net-mvc-3,Asp.net Mvc 3,使用asp.net mvc 3.0,我需要做什么来提供以下路线 public class ProductController : Controller { // ************************ // URL : Product/Create // ************************ public ActionResult Create() { return View(); } // ****

使用asp.net mvc 3.0,我需要做什么来提供以下路线

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

    public ActionResult Create()
    {
       return View();
    }

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

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


Rohan

行动名称不能包含问号。问号是URL中的保留字符,表示查询字符串的开头

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") });
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();
}
或者,如果这些操作有不同的模型,可以执行类似的操作(使用标准的“{controller}/{action}/{optional id}”路由):


如果不创建另一个操作怎么办?只需使用查询字符串调用“创建”操作

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