标签: route-constraint

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

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

例如.网址:

http://mydomain.com/{versionDate}/{controller}/{action}
http://mydomain.com/2010-01-20/search/posts
Run Code Online (Sandbox Code Playgroud)

欢呼:)

asp.net-mvc asp.net-mvc-routing route-constraint

6
推荐指数
1
解决办法
2953
查看次数

首次调用页面上的Url.Action很慢

我有一个相当简单的ASP.MVC视图的性能问题.

这是一个几乎是即时的登录页面,但需要大约半秒钟.

经过大量挖它看起来像这个问题是第一次调用的Url.Action-它采取围绕450ms(根据MiniProfiler),但似乎出奇的慢.

随后的调用时间Url.Action<1ms,这更符合我的预期.

这是我使用的是否是一致的Url.Action("action", "controller")Url.Action("action"),但似乎并没有,如果我使用的情况发生Url.Content("~/controller/action").我打电话时也会发生这种情况Html.BeginForm("action").

有谁知道是什么导致了这个?

一挖成认为,RouteCollection.GetVirtualPath可能是罪魁祸首,因为这是常见的两种Url.ActionHtml.BeginForm.但是,肯定是在整个地方都使用了?我的意思是,½秒太慢了.

我有20个左右的自定义路由(它是一个相当大的应用程序,带有一些遗留的WebForms页面),但即便如此,时间似乎太慢了.

任何想法如何解决它?

asp.net-mvc performance urlhelper route-constraint

6
推荐指数
1
解决办法
3172
查看次数

ASP.NET MVC自定义路由约束和依赖注入

在我的ASP.NET MVC 3应用程序中,我有一个如下定义的路由约束:

public class CountryRouteConstraint : IRouteConstraint {

    private readonly ICountryRepository<Country> _countryRepo;

    public CountryRouteConstraint(ICountryRepository<Country> countryRepo) {
        _countryRepo = countryRepo;
    }

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

        //do the database look-up here

        //return the result according the value you got from DB
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序上使用Ninject作为IoC容器实现IDependencyResolver并且我注册了我的依赖项:

    private static void RegisterServices(IKernel kernel) {

        kernel.Bind<ICountryRepository<Country>>().
            To<CountryRepository>();
    }    
Run Code Online (Sandbox Code Playgroud)

如何以依赖注入友好的方式使用此路由约束?

编辑

我找不到一种方法来传递这种依赖于单元测试:

[Fact]
public void country_route_should_pass() {

    var mockContext = new Mock<HttpContextBase>();
    mockContext.Setup(c => …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc unit-testing dependency-injection route-constraint asp.net-mvc-3

5
推荐指数
2
解决办法
2288
查看次数

POST上的路由约束错误

在操作中操作数据时,通常会收到一个ID作为参数,但是您需要对该ID进行一些错误处理.您必须为每个操作执行的错误处理之一是确保ID高于0(不是负数).因此,我不想在动作中处理这个问题,而是想添加一个路由约束,这样如果它是一个负id,就不会路由到该动作.

这是我的代码:

//route definition
        routes.MapRoute(
            "default route" ,
            "{controller}/{action}/{id}" ,
            new { id = UrlParameter.Optional },
            new { id = @"^\d+$" }
        );

//action definition (note I also tried with only [HttpPost] and with nothing same result
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get )]
        public ActionResult Edit( int id )
Run Code Online (Sandbox Code Playgroud)

当你对动作执行GET操作时,一切正常,但是当我发布POST时,我会得到以下错误,它应该只是转到404页面

 HTTP verb POST used to access path '/object/edit/-2' is not allowed.

[HttpException (0x80004005): The HTTP verb POST used to access path '/object/edit/-2' is not allowed.]
   System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-mvc-routing route-constraint

5
推荐指数
1
解决办法
659
查看次数

我可以在这里使用路线约束吗?

如果我有以下网址:

/ someurl

我有两个域名:

us.foo.com

au.foo.com

我希望这个200(匹配):

us.foo.com/someurl

但这到404(不匹配):

au.foo.com/someurl

路线看起来像这样:

RouteTable.Routes.MapRoute(
   "xyz route",
   "someurl",
   new { controller = "X", action = "Y" }
);
Run Code Online (Sandbox Code Playgroud)

我猜是因为没有路由值,我不能基于主机限制URL?那是对的吗?

如果是这样,我怎么能这样做,除了动作中的以下(丑陋):

if (cantViewThisUrlInThisDomain)
   return new HttpNotFoundResult();
Run Code Online (Sandbox Code Playgroud)

有人有任何想法吗?

我想我有点想通过它的域来限制路由,而不是路由令牌,如果这是有道理的.

asp.net-mvc domain-name asp.net-mvc-routing route-constraint asp.net-mvc-3

4
推荐指数
1
解决办法
521
查看次数