【问题标题】:How to check some criteria before redirection to page using @Html.Actionlink如何在使用@Html.Actionlink 重定向到页面之前检查一些标准
【发布时间】:2012-10-26 06:03:30
【问题描述】:

我使用@Html.ActionLink 移动到一种形式。在转到该页面之前,我想检查一些情况。条件如如果用户对该(编辑)页面具有访问权限,那么只有他可以重定向到编辑页面。

我正在使用 jquery 进行尝试,但默认情况下它只是重定向到编辑页面而不调用 jquery event 。

如何做到这一点?

【问题讨论】:

    标签: asp.net-mvc-3 html.actionlink


    【解决方案1】:

    使用 Jquery

     <a href="@Url.Action("NewLaunches", "Updates")" style="cursor:pointer" id="deleteLink">Delete</a>
    
    $(document).ready(function(){
      $("#deleteLink").click(function(event){
        event.preventDefault();
        bool isallowed= // Logic for finding access permission
        if(isallowed)
        {
        var url = $(this).attr("href"); // picking up the url
        document.location.href=url;
        }
        });});
    

    【讨论】:

    • 不推荐在客户端检查访问权限并授予权限。
    【解决方案2】:

    是的,你可以这样做。

    您说您正在使用@Html.ActionLink 在表单之间移动。所以我假设你的一个 ActionLink 可能看起来像这样......

    @Html.ActionLink("Edit","Home")
    

    你的控制器可能看起来像这样......

    public class HomeController : Controller
    {
        public ActionResult Edit()
        {        
            return View();
        }
    
    }
    

    在 Edit 操作中,您可以检查所需的访问权限,例如:

    public class HomeController : Controller
    {
        public ActionResult Edit()
        {       
            if(Request.IsAuthenticated && SomeOtherCondition()) {
            {
                // if all ok, then forward to Edit page
                return View();
            }
            else{
                // send back to home.
                return("Index");
            }
        }
    
    }
    

    P.S : 我强烈建议您在服务器端而不是客户端 (jquery) 上进行访问权限验证。

    【讨论】:

      【解决方案3】:

      这是服务器逻辑,而不是客户端,尽管如果您使用一些带有密钥、uid 等的客户端安全算法,通过 ajax 与服务器进行主动通信,它会给您带来一些优势。我会推荐你​​:返回RedirectToAction,抛出异常,返回 403 状态码,检查服务器上的权限并且不呈现此链接等等。@amesh 示例将允许用户在浏览器中使用禁用的 javascript 进行导航。

      【讨论】:

        猜你喜欢
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        相关资源
        最近更新 更多