Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:40:11+00:00 2026-05-27T19:40:11+00:00

I need to write the contents of a js file from a convention-driven location

  • 0

I need to write the contents of a js file from a convention-driven location (like ~/ClientApp/Controllers/Home/Home.js if loading the view located at ~/Views/Home/Home.cshtml). How do I do this?

Example: if the file ~/Views/Home/Home.cshtml looks like:

<div id="some-partial-view">
   <!-- ... -->
</div>

And the file ~/ClientApp/Controllers/Home/Home.Controller.js looks like

function HomeController() {
  //some code
}

Then the rendered view returned by the webserver should look like (if using fiddler)

<!--ommitted <html> <body> tags -->

<div id="some-partial-view">
   <!-- ... -->
</div>

<script type="text/javascript">
   function HomeController() {
       //some code
   }
</script>

One way is to add an HTML Helper that will do this such as:

<div id="some-partial-view" ng:Controller="HomeController">
   <!-- ... -->
</div>
@Html.IncludeController("HomeController") 

However, i don’t want to repeat this in all partial views.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:40:12+00:00Added an answer on May 27, 2026 at 7:40 pm

    You could write a custom view:

    public class MyRazorView : RazorView
    {
        public MyRazorView(ControllerContext controllerContext, string viewPath, string layoutPath, bool runViewStartPages, IEnumerable<string> viewStartFileExtensions, IViewPageActivator viewPageActivator)
            : base(controllerContext, viewPath, layoutPath, runViewStartPages, viewStartFileExtensions, viewPageActivator)
        {
    
        }
    
        protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
        {
            base.RenderView(viewContext, writer, instance);
    
            var view = (BuildManagerCompiledView)viewContext.View;
            var context = viewContext.HttpContext;
            var path = context.Server.MapPath(view.ViewPath);
            var viewName = Path.GetFileNameWithoutExtension(path);
            var controller = viewContext.RouteData.GetRequiredString("controller");
            var js = context.Server.MapPath(
                string.Format(
                    "~/ClientApp/Controllers/{0}/{0}.{1}.js",
                    viewName,
                    controller
                )
            );
            if (File.Exists(js))
            {
                writer.WriteLine(
                    string.Format(
                        "<script type=\"text/javascript\">{0}</script>",
                        File.ReadAllText(js)
                    )
                );
            }
        }
    }
    

    and a custom view engine which will return this custom view when a partial view is to be requested:

    public class MyRazorViewEngine : RazorViewEngine
    {
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return new MyRazorView(
                controllerContext, 
                partialPath, 
                null, 
                false, 
                base.FileExtensions, 
                base.ViewPageActivator
            );
        }
    }
    

    which would be registered in Application_Start:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new MyRazorViewEngine());
    }
    

    You probably might need to adjust some of the paths as it was not quite clear in your question where exactly should the js be located but normally you should have enough details in the answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java I need to read a binary file from a site and write
I am downloading a text-file from example.com/test.txt and I need the contents only during
I need to write some data from time to time to a file, appending
I need to write something into a txt file and read the contents, then
Is there any way to view the contents of a vmdk file from Python,
I need to take a message from recv, write it to a text file,
I need to write to a very large text file by adding content to
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a script in Matlab, which will read some data from
I need to write to a text file using JavaScript. I have a machine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.