ASP.NET MVC是否有任何DateTime路由约束?

时间:2022-02-08 14:49:10

does ASP.NET MVC contain any route contraints baked into the code? if so, how do i define a date-time constraint?

ASP.NET MVC是否包含代码中包含的任何路由约束?如果是这样,我如何定义日期时间约束?

eg. url:

http://mydomain.com/{versionDate}/{controller}/{action}
http://mydomain.com/2010-01-20/search/posts

cheers :)

3 个解决方案

#1


10  

I ended up making my own route constraint. only took a few mins.

我最终制定了自己的路线约束。只花了几分钟。

using System;
using System.Web;
using System.Web.Routing;

namespace Whatever.Your.Funky.Cold.Medina.Namespace.Is
{
    public class DateTimeRouteConstraint : IRouteConstraint
    {
        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
                          RouteDirection routeDirection)
        {
            DateTime dateTime;

            return DateTime.TryParse(values[parameterName] as string, out dateTime);
        }

        #endregion
    }
}

simple :P

#2


2  

You could also set up a constraint on the route, something like so. The regular expression used is not very robust, so you should refine it.

你也可以在路线上设置约束,就像这样。使用的正则表达式不是很健壮,因此您应该对其进行优化。

routes.MapRoute( 
    "Version", "
    {versionDate}/{controller}/{action}", 
    new {controller="Search", action="Posts"}, 
    new {versionDate= @"\d\d\d\d-\d\d-\d\d" } 
    ); 

Information from here.

来自这里的信息。

#3


0  

all of the framework is overide-able so it's possible, with a great deal of pain, to overide the default behaviour of the route engine but i agree with @jrista in that you might want to make it a parameter of the controller else mvc will expect to find /search/posts within the 2010-01-20 folder

所有的框架都是可以覆盖的,所以有可能会非常痛苦地覆盖路由引擎的默认行为,但我同意@jrista,因为你可能想让它成为控制器的参数,否则mvc会期望在2010-01-20文件夹中查找/搜索/发布

#1


10  

I ended up making my own route constraint. only took a few mins.

我最终制定了自己的路线约束。只花了几分钟。

using System;
using System.Web;
using System.Web.Routing;

namespace Whatever.Your.Funky.Cold.Medina.Namespace.Is
{
    public class DateTimeRouteConstraint : IRouteConstraint
    {
        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
                          RouteDirection routeDirection)
        {
            DateTime dateTime;

            return DateTime.TryParse(values[parameterName] as string, out dateTime);
        }

        #endregion
    }
}

simple :P

#2


2  

You could also set up a constraint on the route, something like so. The regular expression used is not very robust, so you should refine it.

你也可以在路线上设置约束,就像这样。使用的正则表达式不是很健壮,因此您应该对其进行优化。

routes.MapRoute( 
    "Version", "
    {versionDate}/{controller}/{action}", 
    new {controller="Search", action="Posts"}, 
    new {versionDate= @"\d\d\d\d-\d\d-\d\d" } 
    ); 

Information from here.

来自这里的信息。

#3


0  

all of the framework is overide-able so it's possible, with a great deal of pain, to overide the default behaviour of the route engine but i agree with @jrista in that you might want to make it a parameter of the controller else mvc will expect to find /search/posts within the 2010-01-20 folder

所有的框架都是可以覆盖的,所以有可能会非常痛苦地覆盖路由引擎的默认行为,但我同意@jrista,因为你可能想让它成为控制器的参数,否则mvc会期望在2010-01-20文件夹中查找/搜索/发布