Asp.Net Mvc Url.Action in external js file?

后端 未结 5 1897
再見小時候
再見小時候 2020-11-27 14:57

In external js file, I cant use

url = \"@Url.Action(\"Action\", \"Controller\")\" 
//url output : @Url.Action(\"Action\", \"Controller\")
//I get IllegalPat         


        
5条回答
  •  再見小時候
    2020-11-27 15:40

    As accepted answer described, you can not use any C# code in external JS file. Here is what I normally do if I need to pass few controller links to my external .js file.

    In C# (.cshtml)

    
    
    
    

    In myfile.js

    var _url1;
    var _url2;
    
    function initScript(url1, url2) {
        _url1 = url1;
        _url2 = url2;
    
       // other init. codes...
    
    }
    

提交回复
热议问题