Debugging TypeScript files outside wwwroot

后端 未结 5 831
走了就别回头了
走了就别回头了 2020-12-31 09:40

How should I debug TypeScript files in ASP.NET 5? Suppose solution layout as on the following picture. Notice .ts Scripts are outside wwwroot folder and compiled .js file is

5条回答
  •  醉话见心
    2020-12-31 09:48

    I added the following action to one of my controllers, where appEnvironment is an IApplicationEnvironment injected into your controller:

    #if DEBUG
    
    [ExcludeFromCodeCoverage]
    [Route("Scripts/{*pathInfo}", Order = 500)]
    public IActionResult TestTypescript(string pathInfo, System.Threading.CancellationToken cancellationToken)
    {
        var path = appEnvironment.ApplicationBasePath + "/Scripts/" + pathInfo;
        if (System.IO.File.Exists(path) && pathInfo.EndsWith(".ts"))
        {
            return File(path, "application/javascript");
        }
        else
        {
            return HttpNotFound();
        }
    }
    
    #endif
    

    Conveniently, the new web servers strip out the ../ (I'm assuming for security purposes), so you don't even have to worry about having them in the Route. (Note that you need to not have a [RoutePrefix] attribute on the controller itself for these paths to work.)

提交回复
热议问题