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 9018257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:28:47+00:00 2026-06-16T04:28:47+00:00

I have a JavaScript Function where I want to call a Razor C# Function

  • 0

I have a JavaScript Function where I want to call a Razor C# Function in it, I know it’s easy in HTML but in JavaScript i’m getting many compilation errors I tried the <text> tag trick but nothing worked.

getHTML is My JavaScript Function and getPageSource is a C# function that returns a string:

<script language="javascript" type="text/javascript">

        function getHTML() {
            var html = "@getPageSource("http://www.google.com")" ;       
            alert(html);
        }

</script>

where @getPageSource is defined above the html and is working well within the HTML code

Is there any way to assign some Razor Code to a JavaScript Variable ?

  • 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-06-16T04:28:48+00:00Added an answer on June 16, 2026 at 4:28 am

    You can’t execute server side functions in client side. Razor is only used to give you the ability to have some logic while generating the HTML code to be returned, in the server.

    If that is a C# function that returns a String, you should wrap it inside quotes or it won’t be interpreted as a Javascript string when the browser is parsing your response.

    var html = '@getPageSource("http://www.google.com")';
    

    Remember that if your getPageSource(“http://www.google.com”) returns a string with quotes inside, the javascript will be inconsistent and unable to create your JS string. Imagine that your method returned <div> ' hello ' </div>. That would end up like:

    var html = '<div> ' hello ' </div>';
    

    See the problem?

    See this to convert a HTML C# String into one that can be used as a JS String.
    http://codeasp.net/blogs/raghav_khunger/microsoft-net/722/how-to-escape-single-quotes-in-c-so-that-the-string-can-be-used-in-javascript

    You must replace every ' with \\' . Remember that your string will be completely passed to the HTML code. Imagine that your code was returning "It's blue". When wrote in the HTML it would end as var str = 'It's blue' (notice that "s blue" will be considered outside the string and will give a parsing error by the browser).
    To write that in JS you needed something like var str = 'It\'s blue' so the browser will know that quote is not an ending string quote but it’s contained in the string.
    To include the dash in C#, your returning string must be "It\\'s blue" once the replace of ' with \\'

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

Sidebar

Related Questions

I have a function JavaScript that i want to inject in a Html, with
I have a Javascript function that accepts a list of HTML nodes, but it
I have a javascript function called ShowConfirmationBox(). I want to call it in my
I have one javascript function. Which i want to call on hover of li
I just want to know how to call a javascript function inside another function.
I have a showCustomersList() javascript function in FunctionBag.js now I want to call that
I have a JavaScript function in my html in script tags. I want to
I Have Javascript Function and I Want to call it On each time user
I have JavaScript function call to changeMapLocation function where in it I want to
I want to call a c# function from my javascript function. I have a

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.