简体   繁体   English

控制器为每个页面请求收到2个请求

[英]Controller received 2 request for every page request

I have modified the Map Route in my Dot Net Core application that for every request goes to one controller, so not lots of controller or Action Result is needed for each page that we build. 我已经在Dot Net Core应用程序中修改了“地图路线”,对于每个请求都将其发送给一个控制器,因此,我们构建的每个页面都不需要很多控制器或操作结果。

greedy Routing 贪婪路由

routes.MapRoute("", "/{*article}",
defaults: new { controller = "Pages", action = "Killme" });

In Page Controller build an object that has list of CSS and JavaScript locations, this object is being passed to Page view 在Page Controller中,构建一个具有CSS和JavaScript位置列表的对象,此对象将传递到Page视图

public IActionResult Killme()
{   
 var PageInfo = GetPage_Data();
 ViewBag.PageInfo = t;
 return View("Pages");
}

and i have tried to pass this information as model or view bag to Page view. 我已经尝试将此信息作为模型或视图包传递给Page视图。 in the Page view i try to build the JavaScript and CSS dynamically based on the model like 在页面视图中,我尝试基于以下模型动态构建JavaScript和CSS:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    @{
    RootObject PageInfo = ViewBag.PageInfo;
    foreach (var JavaScript in PageInfo.js)
    {
        var JSsrc = JavaScript.src;
      <script type="text/javascript" src="@JSsrc "> 
                 </script>        

    }
        <script type="text/javascript" src=""></script>
    </head>
    <body>

    </body>
</html>

Now the Issue is when i build the JavaScript the controller is called 2 times and the page is being rendered 2 times, when i comment the 现在的问题是,当我构建JavaScript时,控制器被调用2次,页面被渲染2次,当我评论

 <script type="text/javascript" src="@JSsrc "> 
                     </script> 

the page controller will get called once, any help regarding why this happening and what am i doing wrong would be appreciated 页面控制器将被调用一次,有关这种情况发生的原因以及我在做什么错的任何帮助将不胜感激。

The reason for this behavior is that the staticFileHandler is either not configured or is not able to find your Javascript file and passes the request further down the pipeline. 出现这种现象的原因是未配置staticFileHandler或找不到Javascript文件,无法将请求进一步传递到管道中。 The MVC handler captures all requests (because of your route) and returns therefore the same page again. MVC处理程序捕获所有请求(由于您的路由),因此再次返回同一页面。

First check that your startup class uses the staticFileHandler before the mvcHandler, so something like this: 首先检查您的启动类在mvcHandler之前是否使用了staticFileHandler,如下所示:

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();
        ..
        app.UseMvc();
    }
}

Secondly make sure that your Javascript is placed inside the wwwroot and uses the correct path.So a file like wwwroot/js/mysite.js should a script tag like: 其次,确保将Javascript放在wwwroot内并使用正确的路径,因此wwwroot / js / mysite.js之类的文件应使用如下脚本标记:

<script src="/js/mysite.js"></script>

These improvements should resolve your issue. 这些改进将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM