更新时间:2023-11-25 11:23:04
一种解决方法是在RouteData
中查找特定的控制器.假设您用于主页的控制器称为"HomeController",则请求的RouteData
将包含键"Controller"的值"Home".
One way to approach this would be to look for the specific controller in the RouteData
. Assuming that the controller you are using for the home page is called "HomeController", then the RouteData
for the request would contain the value "Home" for the key "Controller".
它看起来像这样:
而不是(或者除了其他原因之外):
instead of (or in addition to if you have other reasons for it):
@Html.Hidden("returnUrl", Request.Url.AbsoluteUri)
您将拥有:
@Html.Hidden("referrer", Request.RequestContext.RouteData.Values['Controller'])
和您的控制器如下所示:
and your controller would look like:
[HttpPost]
public ActionResult LogOnInt(LogOnModel model)
{
if (model.referrer = "Home")
{
return Json(new { redirectToUrl = @Url.Action("Index","Home")});
}
}
这将消除使用.Contains()
更新 :
Update:
您还可以通过将引荐来源网址(Request.UrlReferrer.AbsoluteUri
)映射到路由来消除对隐藏字段的需求(从而减少应用程序中每个页面的整体页面权重).这里有一篇关于这个的帖子.
You could also eliminate the need for a hidden field (and thereby reduce overall page-weight for what would seem like every page in your application) by mapping the referrer url (Request.UrlReferrer.AbsoluteUri
) to a route. There is a post about that here.
这个想法是使用mvc引擎在LogOnInt
方法中将引荐来源网址映射到MVC路由,从而使代码完全独立.
The idea would be to use the mvc engine to map a referrer url to an MVC route in the LogOnInt
method, allowing the code to be entirely self contained.
这可能比将控制器名称和操作名称放置在那里以供全世界查看以及将其推回服务器的脚本更干净.
This would probably be cleaner than putting the controller name and action name out there for the world to see along with scripting to push it back to the server.