今天在测试自己MVC程序的时候发现之前写代码的一个BUG,需求是每个页面要获取当前URL链接中包含的城市ID,我把获取url的方法写到了Controller的基类BaseController(BaseController继承自Controller),之前写习惯了webForm所以在mvc中写了下面的代码。

public class HomeController : BaseController 
{
......
}
 
public class BaseController : Controller
{
        public BaseController ()
        {
                if (Request==null ) //Request的值始终为null
                {
                    ......
                }
        }
}

解决办法:

public class BaseController : Controller
{
       protected override void OnActionExecuted(ActionExecutedContext ctx) {
            base.OnActionExecuted(ctx);
                        if (Request==null )
                        {
                            ......
                        }
        }
}

 

相关文章:

  • 2021-07-17
  • 2022-01-01
  • 2022-01-02
  • 2022-02-11
  • 2022-12-23
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2022-01-27
  • 2022-01-01
  • 2021-11-14
  • 2021-10-06
  • 2021-11-19
相关资源
相似解决方案