在ASP.NET MVC设定一个备用控制器文件夹位置 [英] Setting an alternate controller folder location in ASP.NET MVC

查看:192
本文介绍了在ASP.NET MVC设定一个备用控制器文件夹位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用的默认文件夹约定的HTML意见,但我们想建立备用服务文件夹仅用于Web服务返回的XML或JSON使用的控制器。一个MVC应用程序

所以路线/服务/任务/列表将被路由到/Services/TaskService.cs,而/任务/列表将被路由到标准的/Controllers/TaskController.cs\"

我们想保持控制器从视图控制器分离的服务。我们不认为区域或使用其他项目工作。什么是接近最好的方法?


解决方案

您可以做到这一点使用的路由,并保持在不同的命名空间的控制器。
图路线可以让你指定的名称空间对应的路由。

示例

鉴于这种控制器

 命名空间CustomControllerFactory.Controllers
{
    公共类HomeController的:控制器
    {
        公众的ActionResult指数()
        {
           返回新ContentResult类型(控制器);
        }
    }
}命名空间CustomControllerFactory.ServiceControllers
{
    公共类HomeController的:控制器
    {
        公众的ActionResult指数()
        {
           返回新ContentResult类型(ServiceControllers);
        }
    }
}

和下面的路由

  routes.MapRoute(
           服务,
           服务/ {控制器} / {行动} / {ID}
            新{控制器=家,行动=索引,ID = UrlParameter.Optional},
            新的String [] {CustomControllerFactory.ServiceControllers} //命名空间
        );
        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional},
            新的String [] {CustomControllerFactory.Controllers} //命名空间
        );

您应该期待以下响应

/服务/首页


  

    

ServiceController的


  

/首页


  

    

控制器


  

We can an MVC app that uses the default folder conventions for the HTML views, but we'd like to set up alternate "Services" folder with controllers used only for web services returning xml or json.

So the route "/Services/Tasks/List" would be routed to "/Services/TaskService.cs", while "/Tasks/List" would be routed to the standard "/Controllers/TaskController.cs"

We'd like to keep the service controllers separate from the view controllers. We don't think areas or using another project will work. What would be the best way to approach this?

解决方案

You can do this using Routing, and keeping the controllers in separate namespaces. MapRoute lets you specify which namespace corresponds to a route.

Example

Given this controllers

namespace CustomControllerFactory.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
           return new ContentResult("Controllers");
        }
    }
}

namespace CustomControllerFactory.ServiceControllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
           return new ContentResult("ServiceControllers");
        }
    }
}

And the following routing

 routes.MapRoute(
           "Services",
           "Services/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "CustomControllerFactory.ServiceControllers" } // Namespace
        );


        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "CustomControllerFactory.Controllers"} // Namespace
        );

You should expect the following responses

/Services/Home

ServiceController

/Home

Controllers

这篇关于在ASP.NET MVC设定一个备用控制器文件夹位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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