ASP.NET MVC:当没有给定参数,URL重定向回到页 [英] ASP.NET MVC: Redirecting back to page when no parameter is given to URL

查看:137
本文介绍了ASP.NET MVC:当没有给定参数,URL重定向回到页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的http://本地主机:50034 /管理/删除/ 723

总是需要这个参数但是要执行的操作,如果你去到URL没有参数发生异常。你如何处理这和重定向回主页面没有做什么吗?

Always needs this parameter to perform the action, however, if you go to the URL without the parameter, an exception occurs. How do you handle this and redirect back to the main page without doing anything?

感谢。

推荐答案

我不知道你的意思,你的意思是,网址 HTTP://本地主机:50034 /管理/删除/ 是产生一个例外?

I am not sure what you mean, do you mean that the url http://localhost:50034/Admin/Delete/ is generating an exception?

尝试设置id参数为空,这样的:

Try setting the id parameter as nullable, like this:

public class MyController : Controller
{
  public void Delete(int? id)
  {
    if (!id.HasValue)
    {
      return RedirectToAction("Index", "Home");
    }

    ///
  }
}

这篇关于ASP.NET MVC:当没有给定参数,URL重定向回到页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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