具有空值的ASP.NET MVC属性路由 [英] ASP.NET MVC Attribute-Routing with Null Values

查看:159
本文介绍了具有空值的ASP.NET MVC属性路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是动作方法MovieCustomer粘贴在EverythingController中. Viewmodel用于合并两个模型:Customer&电影,并通过ApplicationDbContext(_context)从数据库中填充信息.

Here is a paste of the action method MovieCustomer in the EverythingController. The Viewmodel is used to Combine two Models: Customer & Movies, and is populated with information from the Database via the ApplicationDbContext (_context).

当存在MovieId和CustomerId的值时,路由成功运行并呈现页面

例如/Everything/MovieCustomer/1/1

e.g. /Everything/MovieCustomer/1/1

如果一个或两个值都为null,我希望页面也加载.到目前为止,两个int参数都被设置为可为空,并且该方法中有一个if语句,如果其中一个为null,则将参数更改为1. 到目前为止,如果值是null,则浏览器将返回404错误.

I want the page to also load if one or both of the values are null. So far both of the int parameters were made nullable and there is an if statement in the method to change the parameters to 1 if either is null. So far if the values are null the browser returns a 404 error.

当一个或两个参数为null时,如何使页面运行?谢谢

How can I get the page to function when one or either of the parameters are null? Thanks

[Route("Everything/MovieCustomer/{movieId}/{customerId}")]
public ActionResult MovieCustomer(int? movieId, int? customerId)
{
    var viewmodel = new ComboViewModel
    {
        _Customers = new List<Customer>(),
        _Movies = new List<Movies>(),
        _customer = new Customer(),
        _movie =  new Movies()
    };
    viewmodel._Customers = _context.Customers.ToList();
    viewmodel._Movies = _context.Movies.ToList();

    if (!movieId.HasValue)
        movieId = 1;

    if (!customerId.HasValue)
        customerId = 1;

    viewmodel._customer = viewmodel._Customers.SingleOrDefault(a => a.Id == customerId);
    viewmodel._movie = viewmodel._Movies.SingleOrDefault(a => a.Id == movieId);

    return View(viewmodel);
}

推荐答案

您可以使用单独的路由来实现此目的,也可以将参数更改为可选.

You can achieve this using separate routes, or change your parameters to be optional.

使用3个属性时,将为每个具有的选项添加单独的路由-当未指定参数时,仅指定movieId时以及所有3个参数都指定时.

When using 3 attributes, you add separate routes for each of the options that you have - when no parameters are specified, when only movieId is specified, and when all 3 parameters are specified.

[Route("Everything/MovieCustomer/")]
[Route("Everything/MovieCustomer/{movieId}")]
[Route("Everything/MovieCustomer/{movieId}/{customerId}")]
public ActionResult MovieCustomer(int? movieId, int? customerId)
{
    // the rest of the code
}

或者,通过组合操作,您可以将路由参数更改为可选参数(通过在路由定义中添加?),这应该可以覆盖您拥有的所有3种情况:

Alternatively you an combine change your route parameters to optional (by adding ? in route definition) and this should cover all 3 cases that you have:

[Route("Everything/MovieCustomer/{movieId?}/{customerId?}")]
public ActionResult MovieCustomer(int? movieId, int? customerId)
{
    // the rest of the code
}

请记住,这两个示例都不支持仅提供customerId的情况.

Keep in mind that neither sample supports the case where you provide only customerId.

这篇关于具有空值的ASP.NET MVC属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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