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

后端 未结 5 1904
再見小時候
再見小時候 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:22

    I'm not sure if this is the most elegant solution, but what I did was differentiating between registers and the real implementation in the external scripts, so that:

    
    ... include all the external scripts I need
    
    $(document).ready(function(){
    
        //get all the information you need from your MVC context 
        //before going out of context and into the scripts
        var url = '@Url.Action("Action", "Controller")'; 
    
    
         RegisterMyFunction(url, other parameters ..);
         RegisterAnotherFunction(url, others...);
    }
    

    So that in my views I only had the register functions and the scripts contained the special values as a parameter to do whatever I wanted.

    Hope it helps,

提交回复
热议问题