2012-05-19 8 views
20

प्रश्न शीर्षक में बताए गए सरल हैं: क्या 'App_Code' के बाहर रेज़र हेल्पर्स रखने का कोई तरीका है?क्या मेरे पास App_Code के बाहर वैश्विक रेजर @helper हो सकता है?

उदाहरण (HtmlEx.cshtml फ़ाइल):

@helper Script(string fileName, UrlHelper url) 
{ 
<script src="@url.Content("~/Scripts/" + fileName)" type="text/javascript"></script> 
} 

मैं इस पूछना क्योंकि मैं वास्तव में कुछ और App_Code में डालने के लिए नहीं है; मैं अपनी परियोजना को थोड़ा अलग बनाना चाहता हूं।

धन्यवाद।

अद्यतन: मुझे कोई अन्य प्रकार का एक्सटेंशन नहीं चाहिए। मुझे केवल शुद्ध रेजर हेल्पर्स में दिलचस्पी है क्योंकि स्कॉट यहां के बारे में बात कर रहा है: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx

+0

मुझे लगता है कि माइक्रोसॉफ्ट के लोगों को अवधारणाओं को मिश्रण नहीं करना चाहिए। मुझे वास्तव में एक एमवीसी परियोजना में App_Code होने का बिंदु नहीं दिख रहा है। वे हमें वैश्विक रेज़र दृश्यों को लागू करने के लिए लागू करते हैं, और इसके बजाय मैं अंदर एक्सटेंशन भी नहीं डाल सकता (http://stackoverflow.com/questions/3686906/the-call-is-ambiguous-between-the-following-method-or-properties -इन-एएसपी निवल MVC)। अजीब! – Learner

+0

मुझे विश्वास है कि इसे ऐप_Cोड फ़ोल्डर में डालने का कारण है, क्योंकि यह पूरी परियोजना –

+0

पर इंटेलिजेंस प्राप्त करने का एकमात्र तरीका है जब यह ऐप_Cोड में होता है, यह उन्हें स्थिर तरीकों के रूप में बनाता है, और उपयोग करने में सक्षम होने के लिए उन्हें ठीक से हुक करता है सभी प्रासंगिक संदर्भ जहां वे सामान्य रूप से उदाहरण हैं –

उत्तर

17

के रूप में शीर्षक में कहा गया है सवाल सरल है: वहाँ 'App_Code' के बाहर उस्तरा सहायकों होने का एक तरीका है?

नहीं, ऐसा नहीं है।

+0

सरल और प्रभावी उत्तर ... धन्यवाद ... यह देखने के लिए थोड़ा सा खुला रखेगा कि कोई किसी भी तरह के कामकाज जानता है। – Learner

+0

एक सरल, प्रभावी और दुर्भाग्यपूर्ण उत्तर। – Dan

-1

सुनिश्चित करें, आप उन्हें अपने कोड या प्रोजेक्ट स्ट्रक्चर में कहीं भी डाल सकते हैं। फ़ाइल में जहां आप अपना सहायक बनाते हैं, सिस्टम.Web.Mvc का उपयोग करना शामिल करना सुनिश्चित करें।

तो बस सामान्य रूप से इस तरह सहायक वर्ग का विस्तार:

namespace System.Web.Mvc 
{ 
    static class HtmlHelperExtensions 
    { 
     public static IHtmlString MyNewHelper(this HtmlHelper helper, string someParam) 
     { 
      // do something 
     } 
    } 
} 
+2

क्षमा करें, लेकिन मुझे रेज़र हेल्पर्स चाहिए और किसी अन्य प्रकार के एक्सटेंशन (एचटीएमएलहेल्पर और ऐसे) नहीं ... कृपया इसे जांचें: http://weblogs.asp.net/scottgu/archive/ 2011/05/12/एएसपी-नेट-एमवीसी -3-और-द-हेल्पर-सिंटैक्स-भीतर-razor.aspx – Learner

4

Razor Generator एक्सटेंशन का उपयोग अंदर मददगार के साथ एक दृश्य पर करें और आप संकलन समय से पहले दृश्य के लिए कोड उत्पन्न करेंगे। जेनरेटेड व्यू कोड आपकी प्रोजेक्ट का हिस्सा है और असेंबली में संकलित करता है, ताकि आप कहीं भी व्यू फाइल को रख सकें और यूनिट टेस्ट से भी कहीं भी मददगारों का उपयोग कर सकें।

+0

इस अद्यतन के लिए धन्यवाद ... मैं इसे किसी बिंदु पर – Learner

+0

पर आज़माउंगा, यह केवल सचमुच सरल विचारों के लिए काम करता है, अगर आप '" ~/"' शैली यूआरएल या यहां तक ​​कि '@ url.Content (...) 'यह –

+0

काम नहीं करेगा, साथ ही आपको परिवर्तन देखने के लिए हर बार पुन: संकलित करना होगा, लेकिन यह शायद अधिकांश मददगारों के लिए ठीक है - विकास के दौरान केवल दर्द –

6

कभी नेवर से ...

विधि एक: (एक वेब अनुप्रयोग परियोजना में उपयोग के लिए)

बस App_Code फ़ोल्डर में अपने फाइल कॉपी करने के लिए एक पूर्व निर्माण घटना जोड़ें।

(लेकिन चूंकि फ़ाइल शायद परियोजना में शामिल किया जाना चाहिए, आप App_Code निर्देशिका के लिए ही नाम के साथ एक खाली फ़ाइल जोड़ सकते हैं, और फिर इसे अद्यतन करने के लिए निर्माण घटना है।)

(ध्यान दें, भले ही आप मूल रूप से App_code फ़ोल्डर में फ़ाइल डालते हैं, आपको पहली बार इमारत तक इंटेलिजेंस नहीं मिलेगा, इसलिए यह कोई फर्क नहीं पड़ता है।)

विधि दो: (कक्षा पुस्तकालय में उपयोग के लिए, जिसमें स्टार्टअप प्रोजेक्ट एक वेब एप्लीकेशन है)

कक्षा पुस्तकालय में App_ कोड फ़ोल्डर कुछ खास नहीं है, इसलिए सहायक पृष्ठ वैश्विक होने में सक्षम होने के लिए, हमें रेजर कोड को ओवरराइड करना होगा, क्योंकि इसे केवल App_code फ़ोल्डर में कोड के लिए वैश्विक हेल्पर्स बनाने के लिए कोड किया गया है।

और भी रेज़र कोड डिज़ाइन किया गया है ताकि ग्लोबल हेल्पर्स के लिए यह पूर्ण पथ पर आधारित नामस्थान बनाता है, जिसे आप शायद रुचि नहीं रखते हैं।

सब के बाद हम एक समस्या के साथ ही रहते हैं, वहाँ कोई IntelliSense उपलब्ध है कि, इसलिए इन सभी समस्याओं से बचने के लिए, मैं निम्नलिखित कोड लिखा है, यह सोचते हैं कि:

  1. आपका .cshtml (या vbhtml) फ़ाइलें अंतिम परियोजनाओं आउटपुट निर्देशिका
  2. पर प्रतिलिपि बनाई जा रही है आप वैश्विक मददगार फ़ाइल नाम के समान नाम के साथ एक .cs (या .vb) फ़ाइल जोड़ते हैं, और इसे "संकलित" करने के लिए क्रिया निर्माण सेट करते हैं, (यह फ़ाइल स्वतः चालू हो जाएगी इंटेलिजेंस प्रदान करने के लिए स्टार्टअप)
  3. आपको विधानसभाInfo.cs फ़ाइल में PreApplicationStartupClass को पंजीकृत करना है
  4. निर्भरता के क्रम में, बिन फ़ोल्डर में अपने वैश्विक हेल्पर्स पृष्ठ के सापेक्ष पथ देने के लिए आपको प्रीएप्लिकेशंस स्टार्टअपकोड.स्टार्ट() विधि में प्रतिस्थापित करना होगा (यानी। यदि ग्लोबल हेल्पर फाइलों में से एक अन्य फाइल में हेल्पर्स का उपयोग करती है तो इसे इसके बाद सूचीबद्ध किया जाना चाहिए)।
  5. CustomRazorCodeHost वर्ग आप PostProcessGeneratedCode() कि MVC संस्करण के लिए उपयुक्त है की सही विधि लेने के लिए है में स्थापित

यहाँ कोड है (लेकिन एक उचित बयान "का उपयोग" जोड़ना होगा) :

[EditorBrowsable(EditorBrowsableState.Never)] 
public static class PreApplicationStartCode 
{ 
    private static bool _startWasCalled; 

    public static void Start() 
    { 
     // Even though ASP.NET will only call each PreAppStart once, we sometimes internally call one PreAppStart from 
     // another PreAppStart to ensure that things get initialized in the right order. ASP.NET does not guarantee the 
     // order so we have to guard against multiple calls. 
     // All Start calls are made on same thread, so no lock needed here. 

     if (_startWasCalled) 
     { 
      return; 
     } 
     _startWasCalled = true; 

     //Add here the the global helpers based on dependency 
     //also note that each global helper should have a .cs file in the project with the same name 
     CustomRazorHelperBuildProvider bp = new CustomRazorHelperBuildProvider(); 
     bp.VirtualPath = "~/Bin/path/to/helpers/file/Helpers.cshtml"; 
     bp.GenerateCodeAndCompile(); 

     bp = new CustomRazorHelperBuildProvider(); 
     bp.VirtualPath = "~/Bin/path/to/helpers/file/DepndentHelpers.cshtml"; 
     bp.GenerateCodeAndCompile(); 
    } 
} 

public class CustomRazorHelperBuildProvider :RazorBuildProvider 
{ 
    static List<string> GeneratedAssemblyReferences = new List<string>(); 
    public new string VirtualPath { get; set; } 
    protected override System.Web.WebPages.Razor.WebPageRazorHost CreateHost() 
    { 
     return new CustomCodeRazorHost(VirtualPath); 
    } 
    private WebPageRazorHost _host; 
    internal WebPageRazorHost Host 
    { 
     get 
     { 
      if (_host == null) 
      { 
       _host = CreateHost(); 
      } 
      return _host; 
     }    
    } 
    private CodeCompileUnit _generatedCode = null; 
    internal CodeCompileUnit GeneratedCode 
    { 
     get 
     { 
      if (_generatedCode == null) 
      { 
       EnsureGeneratedCode(); 
      } 
      return _generatedCode; 
     } 
    } 
    private CodeDomProvider _provider = null; 
    internal CodeDomProvider Provider 
    { 
     get 
     { 
      if(_provider == null) 
      { 
       _provider = GetProvider(); 
      } 
      return _provider; 
     } 
    } 
    private void EnsureGeneratedCode() 
    { 
     RazorTemplateEngine engine = new RazorTemplateEngine(Host); 
     GeneratorResults results = null; 
     using (TextReader reader = OpenReader(VirtualPath)) 
     { 
      results = engine.GenerateCode(reader, className: null, rootNamespace: null, sourceFileName: Host.PhysicalPath); 
     } 
     if (!results.Success) 
     { 
      RazorError error = results.ParserErrors.Last(); 
      throw new HttpParseException(error.Message + Environment.NewLine, null, VirtualPath, null, error.Location.LineIndex + 1); 
     } 
     _generatedCode = results.GeneratedCode; 
    } 
    private CodeDomProvider GetProvider() 
    { 
     CompilerType compilerType = GetDefaultCompilerTypeForLanguage(Host.CodeLanguage.LanguageName); 
     CodeDomProvider provider = CreateCodeDomProviderWithPropertyOptions(compilerType.CodeDomProviderType); 
     return provider; 
    } 

    /// <summary> 
    /// Generates the c# (or vb.net) code, for the intellisense to work 
    /// </summary> 
    public void GenerateCode() 
    { 
     //Remember that if there is a razor error, then the next time the project will not compile at all, because the generated .cs file will also have the error! 
     //The solution is to add a pre-build event to truncate the file, but not remove it!, also note that the pre-build event will not work in time if the .cs file is open in the VS editor! 
     string filePath = VirtualPath.Replace("/", "\\").Replace("~\\Bin", "").Replace("\\Debug", "").Replace("\\Release", ""); 
     filePath = filePath.Remove(filePath.Length - 4); 
     //filePath = filePath.Insert(filePath.LastIndexOf("\\"), "\\HelperAutoGeneratedCode");    
     Assembly curAssem = Assembly.GetExecutingAssembly(); 
     filePath = HttpRuntime.AppDomainAppPath + "\\..\\" + curAssem.GetName().Name + filePath; 

     using (FileStream fs = new FileStream(filePath, FileMode.Truncate)) 
     { 
      using (StreamWriter sw = new StreamWriter(fs)) 
      { 
       Provider.GenerateCodeFromCompileUnit(GeneratedCode, sw, null);      
       sw.Flush(); 
       sw.Close(); 
      }     
      fs.Close(); 
     } 
     //We need to replace the type of the helpers from "HelperResult" to object, otherwise the intellisense will complain that "it can't convert from HelperResult to object" 
     string text = File.ReadAllText(filePath); 
     text = text.Replace("public static System.Web.WebPages.HelperResult ", "public static object "); 
     File.WriteAllText(filePath, text); 
    } 

    public void GenerateCodeAndCompile() 
    { 
     GenerateCode(); 
     Compile(); 
    } 

    /// <summary> 
    /// Compiles the helper pages for use at runtime 
    /// </summary> 
    /// <returns>Compiler Result</returns> 
    public CompilerResults Compile() 
    { 
     Assembly assem = Assembly.GetExecutingAssembly(); 
     AssemblyName[] references = assem.GetReferencedAssemblies(); 
     List<string> referenceNames = references.Select(r => Assembly.ReflectionOnlyLoad(r.FullName).Location).ToList(); 
     referenceNames.Add(assem.Location); 

     //Add here references that are not included in the project, but are needed for the generated assembly, you can see this through the results.Errors 
     referenceNames.Add((typeof(WebMatrix.Data.ConnectionEventArgs).Assembly.Location)); 
     referenceNames.Add((typeof(WebMatrix.WebData.SimpleRoleProvider).Assembly.Location)); 

     if (GeneratedAssemblyReferences != null && GeneratedAssemblyReferences.Count > 0) 
     { 
      referenceNames.AddRange(GeneratedAssemblyReferences); 
     } 

     CompilerResults results = Provider.CompileAssemblyFromDom(new CompilerParameters(referenceNames.ToArray()), new CodeCompileUnit[] { GeneratedCode }); 
     if (results.Errors.HasErrors) 
     { 
      IEnumerator en = results.Errors.GetEnumerator(); 
      en.MoveNext(); 
      CompilerError error = en.Current as CompilerError; 
      throw new HttpParseException(error.ErrorText + Environment.NewLine, null, VirtualPath, null, error.Line); 
     } 
     Assembly assemblyRef = GetGeneratedType(results).Assembly; 
     GeneratedAssemblyReferences.Add(assemblyRef.Location); //So that any subsequent helper page that is dependent on it will have it as a reference 
     //We need to make it available for Razor, so it will work with reguler razor pages at runtime 
     RazorBuildProvider.CodeGenerationStarted += new EventHandler((sender, args) => (sender as RazorBuildProvider).AssemblyBuilder.AddCodeCompileUnit(this, GeneratedCode)); 
     return results; 
    } 

    private static CodeDomProvider CreateCodeDomProviderWithPropertyOptions(Type codeDomProviderType) 
    { 
     // The following resembles the code in System.CodeDom.CompilerInfo.CreateProvider 

     // Make a copy to avoid modifying the original. 
     var originalProviderOptions = GetProviderOptions(codeDomProviderType); 
     IDictionary<string, string> providerOptions = null; 
     if (originalProviderOptions != null) 
     { 
      providerOptions = new Dictionary<string, string>(originalProviderOptions); 
     } 

     AssemblyName[] references = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); 
     foreach (AssemblyName reference in references) 
     { 
      if (reference.Name == "mscorlib") 
      { 
       providerOptions["CompilerVersion"] = "v" + reference.Version.Major + "." + reference.Version.Minor; 
       break; 
      } 
     } 

     if (providerOptions != null && providerOptions.Count > 0) 
     { 
      ConstructorInfo ci = codeDomProviderType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) }); 
      CodeDomProvider provider = null; 
      if (ci != null) 
      { 
       // First, obtain the language for the given codedom provider type. 
       CodeDomProvider defaultProvider = (CodeDomProvider)Activator.CreateInstance(codeDomProviderType); 
       string extension = defaultProvider.FileExtension; 
       // Then, use the new createProvider API to create an instance. 
       provider = CodeDomProvider.CreateProvider(extension, providerOptions); 
      } 
      return provider; 
     } 

     return null; 
    } 

    internal static IDictionary<string, string> GetProviderOptions(Type codeDomProviderType) 
    { 
     // Using reflection to get the property for the time being. 
     // This could simply return CompilerInfo.PropertyOptions if it goes public in future. 
     CodeDomProvider provider = (CodeDomProvider)Activator.CreateInstance(codeDomProviderType); 
     string extension = provider.FileExtension; 
     if (CodeDomProvider.IsDefinedExtension(extension)) 
     { 
      CompilerInfo ci = CodeDomProvider.GetCompilerInfo(CodeDomProvider.GetLanguageFromExtension(extension)); 
      PropertyInfo pi = ci.GetType().GetProperty("ProviderOptions", 
      BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance); 
      if (pi != null) 
       return (IDictionary<string, string>)pi.GetValue(ci, null); 
      return null; 
     } 
     return null; 
    } 
} 

public class CustomCodeRazorHost : WebPageRazorHost 
{ 
    internal const string ApplicationInstancePropertyName = "ApplicationInstance"; 
    internal const string ContextPropertyName = "Context"; 
    internal const string WebDefaultNamespace = "ASP"; 
    private static readonly string _helperPageBaseType = typeof(HelperPage).FullName; 

    public CustomCodeRazorHost(string virtualPath) 
     : base(virtualPath) 
    { 
     DefaultBaseClass = _helperPageBaseType; 
     DefaultNamespace = WebDefaultNamespace; 
     DefaultDebugCompilation = false; 
     StaticHelpers = true; 
    } 

    //Version for MVC 3 
    public override void PostProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod) 
    { 
     // Add additional global imports 
     generatedNamespace.Imports.AddRange(GetGlobalImports().Select(s => new CodeNamespaceImport(s)).ToArray()); 

     // Create ApplicationInstance property 
     CodeMemberProperty prop = new CodeMemberProperty() 
     { 
      Name = ApplicationInstancePropertyName, 
      Type = new CodeTypeReference(typeof(HttpApplication).FullName), 
      HasGet = true, 
      HasSet = false, 
      Attributes = MemberAttributes.Family | MemberAttributes.Final 
     }; 
     prop.GetStatements.Add(
      new CodeMethodReturnStatement(
       new CodeCastExpression(
        new CodeTypeReference(typeof(HttpApplication).FullName), 
        new CodePropertyReferenceExpression(
         new CodePropertyReferenceExpression(
          null, 
          ContextPropertyName), 
         ApplicationInstancePropertyName)))); 
     generatedClass.Members.Insert(0, prop); 

     // Yank out the execute method (ignored in Razor Web Code pages) 
     generatedClass.Members.Remove(executeMethod); 

     // Make ApplicationInstance static 
     CodeMemberProperty appInstanceProperty = 
      generatedClass.Members 
       .OfType<CodeMemberProperty>() 
       .Where(p => ApplicationInstancePropertyName 
           .Equals(p.Name)) 
       .SingleOrDefault(); 

     if (appInstanceProperty != null) 
     { 
      appInstanceProperty.Attributes |= MemberAttributes.Static; 
     } 
    } 

    //Version for MVC 4 
    public override void PostProcessGeneratedCode(CodeGeneratorContext context) 
    { 
     // Add additional global imports 
     context.Namespace.Imports.AddRange(GetGlobalImports().Select(s => new CodeNamespaceImport(s)).ToArray()); 

     // Create ApplicationInstance property 
     CodeMemberProperty prop = new CodeMemberProperty() 
     { 
      Name = ApplicationInstancePropertyName, 
      Type = new CodeTypeReference(typeof(HttpApplication).FullName), 
      HasGet = true, 
      HasSet = false, 
      Attributes = MemberAttributes.Family | MemberAttributes.Final 
     }; 
     prop.GetStatements.Add(
      new CodeMethodReturnStatement(
       new CodeCastExpression(
        new CodeTypeReference(typeof(HttpApplication).FullName), 
        new CodePropertyReferenceExpression(
         new CodePropertyReferenceExpression(
          null, 
          ContextPropertyName), 
         ApplicationInstancePropertyName)))); 
     context.GeneratedClass.Members.Insert(0, prop); 

     // Yank out the execute method (ignored in Razor Web Code pages) 
     context.GeneratedClass.Members.Remove(context.TargetMethod); 

     // Make ApplicationInstance static 
     CodeMemberProperty appInstanceProperty = 
      context.GeneratedClass.Members 
       .OfType<CodeMemberProperty>() 
       .Where(p => ApplicationInstancePropertyName 
           .Equals(p.Name)) 
       .SingleOrDefault(); 

     if (appInstanceProperty != null) 
     { 
      appInstanceProperty.Attributes |= MemberAttributes.Static; 
     } 
    } 

    protected override string GetClassName(string virtualPath) 
    { 
     return ParserHelpers.SanitizeClassName(Path.GetFileNameWithoutExtension(virtualPath)); 
    } 
} 

लेकिन ध्यान दें कि यदि .cshtml फ़ाइल में एक सिंटैक्स त्रुटि है, तो आप अगली बार संकलित करने के लिए (उत्पन्न .cs फ़ाइल के रूप में त्रुटियों संकलन होगा) समस्याओं नहीं पड़ेगा, हालांकि दृश्य स्टूडियो जाहिरा तौर पर समस्या है समस्या को दूर करने के लिए।

कभी-कभी अंतिम निर्माण (.cs फ़ाइल से संकलित) से संकलित कोड, कभी-कभी नई अद्यतन .cshtml फ़ाइल के साथ संघर्ष कर सकता है।

इसलिए मैं फ़ाइल

echo. > $(ProjectDir)\Path\to\.cs\file 
काटना एक पूर्व निर्माण घटना जोड़ने के लिए सिफारिश करेंगे

आप और अधिक परिष्कृत जाने के लिए और यह कर सकते हैं केवल यदि .cshtml फ़ाइल बदल दिया गया है (और यह भी लागू होता है कोड मैंने ऊपर लिखा है)।

+0

+1 आप निश्चित रूप से इसके लिए सभी योग्यताओं के लायक हैं, लेकिन सुविधा बिंदु से मुझे यकीन नहीं है कि – Learner

+1

वैसे मुझे संदेह है कि यह एक मानक वेब अनुप्रयोग में उपयोगी है, यह स्पष्ट रूप से लाइब्रेरी प्रोजेक्ट में वैश्विक रेज़र हेल्पर्स का उपयोग करने का एकमात्र तरीका है –

संबंधित मुद्दे