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

发布于 2024-08-23 14:38:32 字数 209 浏览 1 评论 0原文

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

例如。网址:

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

干杯:)

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

eg. url:

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

cheers :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送君千里 2024-08-30 14:38:32

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

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

简单:P

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

关于从前 2024-08-30 14:38:32

您还可以对路线设置约束,类似这样。使用的正则表达式不是很健壮,因此您应该对其进行改进。

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

信息来自此处

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.

摘星┃星的人 2024-08-30 14:38:32

所有框架都是可覆盖的,因此有可能覆盖路由引擎的默认行为,但要付出很大的痛苦,但我同意@jrista,因为你可能希望将其作为控制器的参数,否则 mvc 会期望在 2010-01-20 文件夹中找到 /search/posts

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文