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

  • SEARCH
  • Home
  • 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 6069047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:49+00:00 2026-05-23T09:46:49+00:00

I have a javascript file that I am including to a ASP.NET MVC view

  • 0

I have a javascript file that I am including to a ASP.NET MVC view dynamically. This script sets some javascript variables whose values I would like to get from and HTML helper.

The following sets the js variables equal to the string containing the ‘<%’. How should this be formatted so that the server tags are evaluated when this file is included?
Thanks!

 var testPortal = ns.TestPortal;

testPortal.context = {};
testPortal.context.currentUserId = "<%= Html.GetCurrentUserId() %>";
testPortal.context.currentHwid = "<%=Model.Hwid %>"; 
testPortal.context.currentApplicationId = "<%= Html.GetCurrentApplicationId(Model.ProductName) %>";

Update

I am attempting to implement the solution provided to me by Darin below. The new action is working (tested it directly by typing url). However the when I try to create the script tag with a contentPlaceHolder on my view the action is never called. The script tag is never added to the view.

Can someone tell me why this might be??

    <asp:Content ID="Content3" ContentPlaceHolderID="DynamicIncludes" runat="server">
    <script type="text/javascript" src="<%= Url.Action("Index","Context", new{applicationName = Model.ProductName, hwid = Model.Hwid}) %>">
    alert("helloo");
</script>   
    </asp:Content>

Why is the above script not being added?

  • 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-23T09:46:49+00:00Added an answer on May 23, 2026 at 9:46 am

    You cannot use server side includes such as <% %> inside static .js files and that’s because they are not processed by ASP.NET. What you could do instead is to define a controller action or a custom Http handler which will set the response Content-Type HTTP Header to text/javascript which could serve dynamic content. For example:

    For example:

    public ActionResult MyDynamicJs()
    {
        return View();
    }
    

    and inside MyDynamicJs.aspx you could use server side tags:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    <% Response.ContentType = "text/javascript"; %>
    var testPortal = ns.TestPortal;
    testPortal.context = {};
    testPortal.context.currentUserId = "<%= Html.GetCurrentUserId() %>";
    testPortal.context.currentHwid = "<%=Model.Hwid %>"; 
    testPortal.context.currentApplicationId = "<%= Html.GetCurrentApplicationId(Model.ProductName) %>";
    

    Now all that’s left is to include this controller action somwhere:

    <script type="text/javascript" src="<%= Url.Action("MyDynamicJs", "SomeController") %>"></script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asp.net usercontrol that is including some js script like this <script type=text/javascript
I have some code in a javascript file that needs to send queries back
Given that: The asset pipeline is fairly complex (coming from ASP.NET MVC, this is
I have a javascript file that reads another file which may contain javascript fragments
I have a lengthy JavaScript file that passes JSLint except for used before it
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a .tag file that requires a JavaScript library (as in a .js
I have javascript code embedded inside a html template file. When I load this
I have a web control with some custom javascript. The javascript creates new file
I have a javascript routine that dynamically creates an HTML page, complete with it's

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.