简体   繁体   English

带有可选参数的.net MVC操作

[英].net MVC Action with Optional Parameters

I want that controller will accept /Details/1 and /User/xxx but not /User/ 我希望该控制器接受/ Details / 1和/ User / xxx,但不接受/ User /

and i try it like below=> 我尝试如下=>

    public ActionResult Details( Nullable<int> id, Nullable<string> name)
    {
        if (((id ?? 0) == 0) && name == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Instructor instructor = db.Instructors.Where(x => x.InstructorId == id || x.faculty_name.Equals(name)).SingleOrDefault();

        if (instructor == null)
        {
            return HttpNotFound();
        }

        ViewBag.faculty_active = MyCustomFunctions.UserActivity();
        return View(instructor);
    }

i want that(mansion above) user can pass /Details/1 or /Details/xxx but not /Details/ thats why i add condition check=> 我希望(上面的豪宅)用户可以通过/ Details / 1或/ Details / xxx但不能/ Details /多数民众赞成在为什么我要添加条件检查=>

        if (((id ?? 0) == 0) && name == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

but i wants to run the code the compiler gives me error something like=> 但是我想运行编译器给我的错误代码,例如=> 在此处输入图片说明 i was thinking that was perfect but i dont know why a have done wrong = i tried=> 我以为那是完美的,但我不知道为什么做错了=我尝试过=>

    public ActionResult Details( int? id, string? name)
    {
    }

that was also gives me the same error.i searched about this problem and i found=> ASP.NET MVC Overriding Index action with optional parameter 那也给了我同样的错误。我搜索了这个问题,发现=> 具有可选参数的ASP.NET MVC覆盖索引操作

i am not understanding anything . 我什么都不懂。 thats why i reposed this kind of problems. 这就是为什么我摆放这种问题。 this will be great full if anybody can help me? 如果有人可以帮助我,这将是非常丰满的吗?

string is already nullable so Nullable<string> and string? string已经可以为空,所以Nullable<string>string? make no sense. 没道理 However you cannot have 2 nullable parameters and achieve the routes you want. 但是,您不能有2个可为空的参数并实现所需的路由。 The routing engine has no way of knowing which of the parameters should be bound, and if you were to use ../Details/xxx both values would be null and you would always be returning HttpStatusCode.BadRequest . 路由引擎无法知道应该绑定哪个参数,如果您要使用../Details/xxx两个值都将为null并且您始终将返回HttpStatusCode.BadRequest

You could achieve this using 2 separate methods (say DetailsByID and DetailsByName ) and route definitions, but if you want a single method, then you can only have one parameter to account for all 3 routes. 您可以使用2个单独的方法(例如DetailsByIDDetailsByName )和路由定义来实现此目的,但是如果您想要一个方法,则只能使用一个参数来说明所有3条路由。 In the method you can then attempt to parse the value to int 然后在该方法中,您可以尝试将值解析为int

public ActionResult Details(string id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    int number;
    Instructor instructor;
    bool isInt = Int32.TryParse(id, out number);
    if (IsInt)
    {
        instructor = db.Instructors.Where(x => x.InstructorId == number).SingleOrDefault();
    }
    else
    {
        instructor = db.Instructors.Where(x => x.faculty_name.Equals(id)).SingleOrDefault();
    }
    if (instructor == null)
    {
        return HttpNotFound();
    }
    ViewBag.faculty_active = MyCustomFunctions.UserActivity();
    return View(instructor);
}

Don't use Nullable<string> because you do not needed and makes no sense. 不要使用Nullable<string>因为您不需要并且没有任何意义。

string already accepts null value and that means is already nullable . string已经接受null值,这意味着它已经可以为nullable

Short example: 简短示例:

string myString=null;

You need to add Nullable for Value Types, like int,demical,DateTime etc,. 您需要为值类型添加Nullable,例如int,demical,DateTime等。

String is a reference type so it allows null by default 字符串是引用类型,因此默认情况下允许为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM