Home > Software engineering >  If Id does not come, it returns an error, but if Id does not come, I want it to redirect
If Id does not come, it returns an error, but if Id does not come, I want it to redirect

Time:07-16

enter image description here

If Id does not come, it returns an error, but if Id does not come, I want it to redirect.

CodePudding user response:

You can declare your id as nullable:

public ActionResult Detail(Guid? id)
{
  if (!id.HasValue)
    return RedirectToAction( /* ... * ); // or some other form of redirect
  // ... access value of id like this: id.Value
}

CodePudding user response:

You need to set the Id argument as optional somehow.

Either:

  • Give it a default value: Guid Id = Guid.Empty, and check in the body of the method whether it is in fact Guid.Empty.
  • Make it nullable: Guid? Id, and add a null-check

CodePudding user response:

Make the parameter nullable:

public ActionResult Detail(Guid? id)

And redirect if it's null:

if (id == null)
    return RedirectToAction("SomeAction");
  • Related