控制器在单独的程序和路由 [英] Controller in separate assembly and routing

查看:118
本文介绍了控制器在单独的程序和路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同一个解决方案,有一个ASP.NET应用程序MVC4 Slick.App 和类库 Awesome.Mvc.Lib 。 Awesome.Mvc.Lib包含一个控制器类。

In the same solution, there is a ASP.NET MVC4 application Slick.App and class library Awesome.Mvc.Lib. Awesome.Mvc.Lib contains one controller class.

public class ShinnyController : Controller
{
    [HttpGet]
    public string Index()
    {
        return "Hello, from Awesome.Mvc.Lib";
    }
}

如果我刚从Slick.App添加引用Awesome.Mvc.Lib,运行应用程序并点布劳尔到 /晶灵,我会真正看到的反应你好,从Awesome.Mvc.Lib。

If I just add the reference from Slick.App to Awesome.Mvc.Lib, run the application and point brower to /shinny, I will actually see the response "Hello, from Awesome.Mvc.Lib".

这是我不希望的。所有我认为ASP.NET MVC的时间尊重命名空间放置有。所以,从另外一个命名空间的控制器不暴露控制器,至少之前我也没问到。

This is something I don't expect at all. All the time I thought ASP.NET MVC respects the namespaces there the controllers placed in. So, the controllers from another namespaces are not exposed, at least before I didn't ask to.

我试图改变缺省路由的注册,使用的命名空间参数。

I tried to change the default route registration, to use namespaces parameter.

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new [] { "Slick.App.Controllers" }
    );

不过,ShinnyController路线仍然匹配/晶灵。

Still, the ShinnyController route still match for '/shinny'.

我有一个担心,这是正确的默认行为。我的问题是,如何明确说哪些控制器暴露和prevent缺省路由匹配单独的类库控制器?

I have a concerns this is right default behaviour. My question is, how to explicitly say which controllers are exposed and prevent default route to match controllers in separate class library?

推荐答案

该航线的命名空间列表仅给出的优先级以一定的命名空间过别人,这是不列出的:

The namespaces list on the route only gives priority to certain namespaces over the others, which are not listed :

new [] {"Namespace1", "Namespace2"}

并不Namespace1给予更高的优先级为人们所期望的只是优先两个命名空间比其他人。

doesn't give higher priority to Namespace1 as one would expect but just gives priority to both namespaces over the others.

这意味着,在列表中的名称空间的第一搜索控制器,然后如果没有找到匹配使用该名称的可用控制器其余的都用。

This means that the namespaces in the list are first searched for controllers and then, if no match is found the rest of the available controllers with that name are used.

您可以通过这样做晚饭preSS采用非优先控制器:

You can suppress the use of non prioritized controllers by doing this:

var myRoute  = routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new [] { "Slick.App.Controllers" }
    );

myRoute.DataTokens["UseNamespaceFallback"] = false;

这篇关于控制器在单独的程序和路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆