Asp.net mvc MVC5应用程序的角度Cli延迟加载
我正在使用Angular 4构建MVC5应用程序,并使用Angular Cli创建Angular应用程序 我面临的问题是,MVC5应用程序在端口5011上运行,而angular应用程序使用angular cli使用端口4200。当我运行MVC应用程序时,除了延迟加载的模块外,其他一切都正常工作 因为延迟加载的模块创建chunks.js,并且这些chunks给出了未找到的错误。但所有其他js都已成功加载 下面是加载chunks.js的问题 我的MVC应用程序使用端口5011 Angular cli应用程序使用端口4200 我在my_Layout.cshtml中引用js文件如下Asp.net mvc MVC5应用程序的角度Cli延迟加载,asp.net-mvc,angular,angular-cli,Asp.net Mvc,Angular,Angular Cli,我正在使用Angular 4构建MVC5应用程序,并使用Angular Cli创建Angular应用程序 我面临的问题是,MVC5应用程序在端口5011上运行,而angular应用程序使用angular cli使用端口4200。当我运行MVC应用程序时,除了延迟加载的模块外,其他一切都正常工作 因为延迟加载的模块创建chunks.js,并且这些chunks给出了未找到的错误。但所有其他js都已成功加载 下面是加载chunks.js的问题 我的MVC应用程序使用端口5011 Angular cli
<script type="text/javascript" src="http://localhost:4200/inline.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/polyfills.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/styles.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/vendor.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/main.bundle.js"></script>
为什么chunks端口与Angular cli端口不同?如何解决此问题?1)在web.config中添加以下内容
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0_2"
path="/*"
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
告诉路由器将所有请求重定向到主控制器的索引
3) 在HomeController索引方法中添加以下内容
public ActionResult Index()
{
var a = Request.Url.AbsoluteUri.ToString();
var b = Request.RawUrl.ToString();
if(b!="/") return Redirect("http://localhost:4200"+b);
return View();
}
告诉index方法将包含任何其他文件名的任何请求重定向到
本地主机:4200
实际发生的是我们的index.cshtml正在向自身发出请求,而我们的块正在其他服务器上服务,即localhost:4200,
所以我们在这里要做的是,我们将所有包含其路径中的块的请求重定向到localhost:4200
它是经过测试和验证的工作解决方案。我不明白为什么angular dev服务器与本地IIS Express一起运行?我假设您的工作流程是,您通过ng build构建angular应用程序,然后通过端口5011在IIS Express上运行它?如果它解决了您的问题,我会更高兴,我已经测试了它,它工作正常,对您有效吗?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Redirect",
url: "{*.url}",
defaults: new { controller = "Home", action = "Index" }
);
}
public ActionResult Index()
{
var a = Request.Url.AbsoluteUri.ToString();
var b = Request.RawUrl.ToString();
if(b!="/") return Redirect("http://localhost:4200"+b);
return View();
}