Asp.net mvc MVC-返回基于管线的视图

Asp.net mvc MVC-返回基于管线的视图,asp.net-mvc,Asp.net Mvc,在MVC中,如何根据用于访问控制器/操作的路由返回视图 我想为我的站点的另一部分重用控制器/操作,但我想返回稍微不同的视图 因此,例如,这两个URL应该命中同一个控制器,但返回不同的视图: /管理员/报告/票据 /client/admin/report/sales将控制器操作的“肉”放入两个控制器操作调用的另一个类中 例如: // /admin/reports/tickets public class ReportsController : Controller { public Act

在MVC中,如何根据用于访问控制器/操作的路由返回视图

我想为我的站点的另一部分重用控制器/操作,但我想返回稍微不同的视图

因此,例如,这两个URL应该命中同一个控制器,但返回不同的视图:

/管理员/报告/票据
/client/admin/report/sales

将控制器操作的“肉”放入两个控制器操作调用的另一个类中

例如:

// /admin/reports/tickets
public class ReportsController : Controller
{
    public ActionResult Tickets(int customerId)
    {
        using (var db = new MyDataContext())
        {
            var purchaseTransactions = db.Transactions
                .Where(trx => 
                    trx.Type == 'P'  // Purchase
                    && trx.CustomerId == customerId)
            return View(purchaseTransactions);
        }
    }
}

// /client/admin/report/sales
public class ReportController : Controller
{
    public ActionResult Sales(int customerId)
    {
        using (var db = new MyDataContext())
        {
            var purchaseTransactions = db.Transactions
                .Where(trx => 
                    trx.Type == 'P'  // Purchase
                    && trx.CustomerId == customerId)
            return View(purchaseTransactions);
        }
    }
}
变成:

public class TicketService
{
    public IEnumerable<Transaction> GetTicketPurchasesForCustomer(int customerId)
    {
        using (var db = new MyDataContext())
        {
            var purchaseTransactions = db.Transactions
                .Where(trx => 
                    trx.Type == 'P'  // Purchase
                    && trx.CustomerId == customerId)
            return purchaseTransactions;
        }
    }
}

// /admin/reports/tickets
public class ReportsController : Controller
{
    public ActionResult Tickets(int customerId)
    {
        TicketService svc = new TicketService();
        IEnumerable<Transaction> tickets = svc.GetTicketPurchasesForCustomer(customerId);
        return View(tickets);
    }
}

// /client/admin/report/sales
public class ReportController : Controller
{
    public ActionResult Sales(int customerId)
    {
        TicketService svc = new TicketService();
        IEnumerable<Transaction> sales = svc.GetTicketPurchasesForCustomer(customerId);
        return View(sales);
    }
}
公共类票务服务
{
公共IEnumerable GetTicketPurchasesForCustomer(int customerId)
{
使用(var db=new MyDataContext())
{
var purchaseTransactions=db.Transactions
.其中(trx=>
trx.Type='P'//购买
&&trx.CustomerId==CustomerId)
退货交易;
}
}
}
///admin/reports/tickets
公共类报表控制器:控制器
{
公共行动结果票证(int customerId)
{
TicketService svc=新TicketService();
IEnumerable tickets=svc.GetTicketPurchasesForCustomer(customerId);
回程票;
}
}
///client/admin/report/sales
公共类ReportController:控制器
{
公共行动结果销售(int customerId)
{
TicketService svc=新TicketService();
IEnumerable sales=svc.GetTicketPurchasesForCustomer(customerId);
退货视图(销售);
}
}

您可以为此场景配置路由,如下所示:

routes.MapRoute(
    name: "tickets",
    url: "admin/reports/tickets",
    defaults: new { controller = "Admin", action = "Tickets" }
);

routes.MapRoute(
    name: "sales",
    url: "client/admin/report/sales",
    defaults: new { controller = "Admin", action = "Sales" }
);
因此,每个路由都会导航到同一个控制器,但会使用不同的操作方法,从而可以呈现不同的视图

public class AdminController : Controller
{
    public ActionResult Tickets() {}
    public ActionResult Sales() {}
}

我建议将动作方法分开,不要试图聪明地使用相同的动作方法,然后使用一些逻辑来确定要显示什么。它只会把事情弄得一团糟。

好吧,这可能是最好的主意,因为当我稍后回到代码时,它不会造成混乱。