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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:23:58+00:00 2026-06-18T02:23:58+00:00

How can you point directly to a .cshtml view from javascript file? For example

  • 0

How can you point directly to a .cshtml view from javascript file?
For example why can’t I use a .cshtml view with angular.js?
like in this example:

 .directive('encoder', ($timeout) => {
        return {
            restrict: 'E',
            transclude: true,
            scope: 'isolate',
            locals: { service: 'bind' },
            templateUrl: 'encoderTemplate.cshtml' // <-- that's not possible?
        }
    });

It’s of course possible to have an action method that returns whatever you want, but I’m curious if it’s possible to reference directly to the razor view.

  • 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-18T02:23:59+00:00Added an answer on June 18, 2026 at 2:23 am

    As mentioned in a comment, you can’t serve up .cshtml files directly, However, you can use a controller to render the content if you so choose:

    public class TemplateController : Controller
    {
        // create a ~/Views/Template/Encoder.cshtml file
        public PartialViewResult Encoder()
        {
            return PartialView();
        }
    }
    

    Then reference it like you would with @Url.Action:

    {
        ....
        templateUrl: '@Url.Action("Encoder", "Template")'
    }
    

    From Comment

    If you have most of the JavaScript code outside of something that has Razor access (e.g. external .js file) you can still take advantage of the Url builder, just have to do so a little differently. For instance, I may do something like:

    public class TemplateController : Controller
    {
        // Add a child method to the templates controller that outputs default
        // configuration settings (and, since it's a child action, we can re-use it)
        [ChildActionOnly]
        public PartialViewResult Index()
        {
            // You could build a dynamic IEnumerable<ConfigRef> model
            // here and pass it off, but I'm just going to stick with a static view
            return PartialView();
        }
    }
    

    ~/Views/Template/Index.cshtml

    <script type="text/javascript">
      if (typeof window.App === 'undefined'){
        window.App = {};
      }
      App.Templates = {
        Encoder: '@Url.Action("Encoder", "Template")',
        Template1: '@Url.Action("Template1", "Template")',
        Template2: '@Url.Action("Template2", "Template")'
      };
    </script>
    @*
       the template files would then reference `App.Templates.Encoder`
       when they need access to that template.
    *@
    @Scripts.Render("~/js/templating")
    

    Index.cshtml (or whatever is any view)

    @* ... *@
    @{ Html.RenderAction("Index", "Template"); }
    @* ... *@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If some one can point me in the right direction for this code for
Can anyone point out the flaw in this code? I'm retrieving some HTML with
Someone can point me reliable method to run given application in different session of
I wonder if anyone can point me in the right direction I'm fairly new
Hoping someone can point me in the right direction. I'm building a online shop
i hope someone can point me in the right direction On a existing html
Was wondering if anyone can point me to an open source java quant library
I'm curious if someone can point me in the right direction here. I'm learning
I'm writing an application where user can point a position in a Google Maps
I was wondering if anyone can point me in the right direction. I have

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.