0

Is there an easy way to reference JS sources from a JS project that has "JSProj/src/main.js" , "JSProj/package.json" to run at "AspNetCoreProj/startup.cs"?

So that when the asp is run, my controller from "AspNetCoreProj/Controllers/MyController.cs" binds to (launchSettings.json) "applicationUrl": "http://localhost:44709", "sslPort": 46367 AND the front-end JS App is run at, let's say, http://localhost:8080 at the same time?

So that I only need to run the asp app, not the two applications, including AspNetCoreProj and JSProj, separately.

I already installed node.js and run the JS app server using npm run build / npm run serve / yarn serve from command line, but I do not understand the concept of deploying the JavaScript sources (especially when it uses some framework like Angular) along with my ASP project, it just doesn't compile and/or run (that is to say: I'm very new to JS and its frameworks concept).

Do I really need to run a separate JS server for front-end or I can easily reference a JS project in a let's say, index.html / index.cshtml page?

edit: I need to know how to bind an asp.net core project with an angular (react.js / vue.js) project.

2
  • 1
    Do you ask about how to bind your asp.net core project with an angular project specifically? if yes please add angular tag to your question and no need for asp.net-mvc tag. Commented Nov 29, 2021 at 15:02
  • @MernaMustafa, yes, I try to bind Angular project to an ASPNetCore project, but I also need to know how to bind a vue.js project to ASPNetCore project, is there a common way to do that? Commented Nov 29, 2021 at 16:05

2 Answers 2

1

When a JS framework runs using serve, it will run on its own in-memory server like in angular a node.js server with its own port ,one option like you mentioned is to run manually both server and js framework, another option in ASP.NET Core is to "tell" the server to run the JS framework.

In any of this options (while developing) the JS framework will run from its own local server (in Angular a node.js) it is just that in second option the ASP.NET Core will do the serve for you, for this to happened you need to "tell" the asp core application what to do on startup, in order to do that in you need to configure startup.cs.

I think the best way is to open a new Angular project from template project in Visual Studio you can see all the settings that are needed, I will just mention that there are settings in 3 places: in startup.cs in ConfigureServices() and in Configure() as well as right click on the ASP.NET Core project => edit project there are some commands how to handle the Angular app when compiling as well some publish commands

Sign up to request clarification or add additional context in comments.

1 Comment

Actualy if there is a need to use the formar asp mvc app with angular ,you should configure the RouteConfig.cs and to dedicate a controler to return the index.html for example : the controller will be public ActionResult Index() { return new FilePathResult("~/my_ng_App/index.html","text/html"); }
0

In fact the problem was that an MVC (Web Controller app) project cannot be used to run JavaScript, It needs to be an "Angular" asp.net core web app or an asp.net core web app that's configured to use a single-page application (SPA) in startup.cs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.