C# 获取在ASP.NET MVC中调用控制器的视图的名称

C# 获取在ASP.NET MVC中调用控制器的视图的名称,c#,asp.net-mvc,view,controller,asp.net-mvc-5,C#,Asp.net Mvc,View,Controller,Asp.net Mvc 5,有没有办法获取在控制器中调用方法的视图的名称,并将其保存在该控制器方法内的某个自定义变量中 例如: 我有一个视图使用Ajax访问controller中的InfinateScroll方法: <div class="container-post"> <div id="postListDiv"> @{Html.RenderAction("PostList", "Posts", new { Model = Model });} </div&g

有没有办法获取在控制器中调用方法的视图的名称,并将其保存在该控制器方法内的某个自定义变量中

例如:

我有一个视图使用Ajax访问controller中的InfinateScroll方法:

<div class="container-post">
    <div id="postListDiv">
        @{Html.RenderAction("PostList", "Posts", new { Model = Model });}
    </div>
    <div id="loadingDiv" style="text-align: center; display: none; margin-bottom: 20px;">
        <img alt="Loading" src="@Url.Content("~/images/ajax-loader.gif")" />
    </div>
</div>

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
    <script type="text/javascript">
        var BlockNumber = 2;
        var NoMoreData = false;
        var inProgress = false;

        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height() && !NoMoreData && !inProgress) {
                inProgress = true;
                $("#loadingDiv").show();

                $.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber },
                    function (data) {
                        BlockNumber = BlockNumber + 1;
                        NoMoreData = data.NoMoreData;
                        $("#postListDiv").append(data.HTMLString);
                        $("#loadingDiv").hide();
                        inProgress = false;
                    });
            }
        });
    </script>
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber, int userId)
{
    //filter your data based on the "userId" parameter
}

@{Html.RenderAction(“PostList”,“Posts”,new{Model=Model});}
var BlockNumber=2;
var NoMoreData=假;
var inProgress=假;
$(窗口)。滚动(函数(){
if($(窗口).scrollTop()==$(文档).height()-$(窗口).height()&&&!NoMoreData&&!inProgress){
inProgress=true;
$(“#加载div”).show();
$.post(@Url.Action(“InfinateScroll”,“Posts”),{“BlockNumber”:BlockNumber},
功能(数据){
BlockNumber=BlockNumber+1;
NoMoreData=data.NoMoreData;
$(“#postListDiv”).append(data.HTMLString);
$(“#加载div”).hide();
inProgress=假;
});
}
});
我在两个页面上使用此视图。在一个例子中,我使用它只显示来自特定用户(已登录用户)的帖子,在另一个视图中,我显示来自数据库中所有用户的帖子(类似于Facebook墙,在这里你只能看到你的帖子,在新闻提要中,你不仅可以看到你的帖子,还可以看到你朋友的帖子)

出于某种原因,我想知道调用InfinateScroll方法时哪个页面处于活动状态

这是我想在这两个页面之间做一些区别的方法,这样我可以在以后做一些检查

[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
     int BlockSize = 5;
     var posts = PostManager.GetPosts(BlockNumber, BlockSize);
     JsonModel jsonModel = new JsonModel();
     jsonModel.NoMoreData = posts.Count < BlockSize;
     jsonModel.HTMLString = RenderPartialViewToString("PostList", posts);

     return Json(jsonModel);
}
[HttpPost]
公共行动结果InfinateScroll(int BlockNumber)
{
int BlockSize=5;
var posts=PostManager.GetPosts(BlockNumber,BlockSize);
JsonModel JsonModel=新的JsonModel();
jsonModel.NoMoreData=posts.Count

此方法使用助手方法GetPosts获取帖子,并用于在滚动上显示更多帖子。

您可以在html中创建一个隐藏变量,如下所示-

<input type="hidden" id="pageName" value="myPage1" />
然后,在jquery代码中,当您发布时,也发送pageName

$.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber, "pageName": $('#pageName').val() },

希望这有帮助。

您可以在html中创建一个隐藏变量,如下所示-

<input type="hidden" id="pageName" value="myPage1" />
然后,在jquery代码中,当您发布时,也发送pageName

$.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber, "pageName": $('#pageName').val() },

希望这有帮助。

您可以使用以下命令从视图内部获取当前视图的名称:

@Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))
资料来源:

因此,您可以将其作为routevalue添加到@Url.Action中,如下所示:

@Url.Action(
    "InfinateScroll",
    "Posts",
    new{callingView=Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))})
然后,您可以向控制器方法添加一个参数

public ActionResult InfinateScroll(int BlockNumber, string callingView)

可以使用以下命令从视图内部获取当前视图的名称:

@Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))
资料来源:

因此,您可以将其作为routevalue添加到@Url.Action中,如下所示:

@Url.Action(
    "InfinateScroll",
    "Posts",
    new{callingView=Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))})
然后,您可以向控制器方法添加一个参数

public ActionResult InfinateScroll(int BlockNumber, string callingView)
在一个例子中,我用它只显示来自特定用户的帖子。。。和 在另一个视图中,我显示了数据库中所有用户的帖子

将所需逻辑放在视图上是不安全的,尤其是在显示数据是基于用户或特定于用户的情况下。但是,如果坚持在视图上使用逻辑,则应将另一个变量传递给控制器,如下所示:

$.post("@Url.Action("InfinateScroll", "Posts")", 
{ "BlockNumber": BlockNumber, "UserId": userId },
    // rest of your code goes here...
});
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
    // a context based logic
    var userId = GetLoggedInUserId();
    // that method could return null or zero 
    // and depending on how you approach it       
    //filter your data based on the "userId" 
}
然后,控制器中应该有另一个参数:

<div class="container-post">
    <div id="postListDiv">
        @{Html.RenderAction("PostList", "Posts", new { Model = Model });}
    </div>
    <div id="loadingDiv" style="text-align: center; display: none; margin-bottom: 20px;">
        <img alt="Loading" src="@Url.Content("~/images/ajax-loader.gif")" />
    </div>
</div>

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
    <script type="text/javascript">
        var BlockNumber = 2;
        var NoMoreData = false;
        var inProgress = false;

        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height() && !NoMoreData && !inProgress) {
                inProgress = true;
                $("#loadingDiv").show();

                $.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber },
                    function (data) {
                        BlockNumber = BlockNumber + 1;
                        NoMoreData = data.NoMoreData;
                        $("#postListDiv").append(data.HTMLString);
                        $("#loadingDiv").hide();
                        inProgress = false;
                    });
            }
        });
    </script>
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber, int userId)
{
    //filter your data based on the "userId" parameter
}
但正如我提到的,这是不安全的,因为有人可以很容易地传递一个有效的“用户ID”,并在您不希望他们访问数据时访问数据。因此,最安全(或更安全)的方法是在控制器中设置“过滤逻辑”,如下所示:

$.post("@Url.Action("InfinateScroll", "Posts")", 
{ "BlockNumber": BlockNumber, "UserId": userId },
    // rest of your code goes here...
});
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
    // a context based logic
    var userId = GetLoggedInUserId();
    // that method could return null or zero 
    // and depending on how you approach it       
    //filter your data based on the "userId" 
}
在一个例子中,我用它只显示来自特定用户的帖子。。。和 在另一个视图中,我显示了数据库中所有用户的帖子

将所需逻辑放在视图上是不安全的,尤其是在显示数据是基于用户或特定于用户的情况下。但是,如果坚持在视图上使用逻辑,则应将另一个变量传递给控制器,如下所示:

$.post("@Url.Action("InfinateScroll", "Posts")", 
{ "BlockNumber": BlockNumber, "UserId": userId },
    // rest of your code goes here...
});
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
    // a context based logic
    var userId = GetLoggedInUserId();
    // that method could return null or zero 
    // and depending on how you approach it       
    //filter your data based on the "userId" 
}
然后,控制器中应该有另一个参数:

<div class="container-post">
    <div id="postListDiv">
        @{Html.RenderAction("PostList", "Posts", new { Model = Model });}
    </div>
    <div id="loadingDiv" style="text-align: center; display: none; margin-bottom: 20px;">
        <img alt="Loading" src="@Url.Content("~/images/ajax-loader.gif")" />
    </div>
</div>

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
    <script type="text/javascript">
        var BlockNumber = 2;
        var NoMoreData = false;
        var inProgress = false;

        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height() && !NoMoreData && !inProgress) {
                inProgress = true;
                $("#loadingDiv").show();

                $.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber },
                    function (data) {
                        BlockNumber = BlockNumber + 1;
                        NoMoreData = data.NoMoreData;
                        $("#postListDiv").append(data.HTMLString);
                        $("#loadingDiv").hide();
                        inProgress = false;
                    });
            }
        });
    </script>
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber, int userId)
{
    //filter your data based on the "userId" parameter
}
但正如我提到的,这是不安全的,因为有人可以很容易地传递一个有效的“用户ID”,并在您不希望他们访问数据时访问数据。因此,最安全(或更安全)的方法是在控制器中设置“过滤逻辑”,如下所示:

$.post("@Url.Action("InfinateScroll", "Posts")", 
{ "BlockNumber": BlockNumber, "UserId": userId },
    // rest of your code goes here...
});
[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
    // a context based logic
    var userId = GetLoggedInUserId();
    // that method could return null or zero 
    // and depending on how you approach it       
    //filter your data based on the "userId" 
}

首先,为了澄清问题,视图不会“调用”控制器。用户对视图执行操作,该操作通常映射到ASP.NETMVC中表示为路由的服务器端资源。然后将此路由映射到控制器上的相应操作。第二,如何调用控制器操作?这是一个AJAX请求,是一个表单请求,使用了什么HTTP动词?@swazza85我将用示例更新我的问题,更清楚地说,因为我问错了:)@swazza85我添加了一些代码。使用Ajax请求调用控制器操作。希望我没有太复杂。实际上,我应该是一个非常简单的任务,但我不太擅长解释问题。首先,为了澄清问题,视图不会“调用”控制器。用户对视图执行操作,该操作通常映射到ASP.NETMVC中表示为路由的服务器端资源。然后将此路由映射到控制器上的相应操作。第二,如何调用控制器操作?这是一个AJAX请求,是一个表单请求,使用了什么HTTP动词?@swazza85我将用示例更新我的问题,更清楚地说,因为我问错了:)@swazza85我添加了一些代码。使用Ajax请求调用控制器操作。希望我没有太复杂。实际上,我的任务应该很简单,但我不太擅长解释问题。另一方面,一般的惯例是使用驼峰式大小写来用C#o命名方法参数