[英]Using Visual Studio to make non aspx code-behind pages
I want to build my own "code behind" like pages so that i can have HTML in a HTML file and code in cs file but be able to have Intellesense for the tokens in the HTML file.我想构建自己的“代码背后”,如页面,这样我就可以在 HTML 文件中使用 HTML 并在 cs 文件中使用代码,但能够对 HTML 文件中的令牌使用 Intellesense。 (i know that's what the .NET page class does, but i want to have something much lighter)
(我知道这就是 .NET 页面 class 所做的,但我想要更轻的东西)
EG: in the.html file: EG:在.html文件中:
<%@ Directive classname="HTMLSnippet" %>
<html>
<body>
<div>[%message%]
</body>
</html>
and in a.cs file并在 a.cs 文件中
public class MyClass : HTMLSnippet
{
public class MyClass () {
snippet.message = "message goes here"
}
}
So my question is how do make the HTMLSnippet class so that it's members are automatically created, and specifically show up in Intellesense as i add tokens to the.html file?所以我的问题是如何制作 HTMLSnippet class 以便自动创建它的成员,并在我将令牌添加到 .html 文件时特别显示在 Intellesense 中?
I know that .net currently does it by creating the designer.cs file and basically builds a class with all the elements from the page as it goes, and that would work fine but how can i get visual studio to generate that before compiling so that it shows up in Intellesense.我知道.net目前通过创建designer.cs文件来完成它,并且基本上构建了一个class,其中包含页面中的所有元素,这可以正常工作,但是我怎样才能让Visual Studio在编译之前生成它以便它出现在 Intellesense 中。 Thanks!
谢谢!
Clarification I'm not using this as a handler yet, i want to use this to have HTML snippets with tokens be usable in code as an object with properties.澄清我还没有将其用作处理程序,我想使用它来使带有令牌的 HTML 片段可在代码中用作具有属性的 object。 so almost like a custom control.
所以几乎就像一个自定义控件。 I think what i have to do is create a VS add-in that waits for me to type tokens into an.html file then it automatically creates a.cs file with members for each token.
我认为我要做的是创建一个 VS 加载项,等待我将令牌输入到一个 .html 文件中,然后它会自动创建一个包含每个令牌成员的 a.cs 文件。
First, there is not a lot of bloat in an ASPX page, if you don't want the bloat.首先,如果您不想膨胀,那么 ASPX 页面中不会有太多膨胀。 You can get rid of pretty much everything other than the @ Page directive tag.
除了@ Page 指令标记之外,您几乎可以摆脱所有东西。 You can turn off viewstate, as well, if you do not want bloat on the client side, so I see no reason to reinvent the wheel.
如果您不想在客户端膨胀,您也可以关闭视图状态,所以我认为没有理由重新发明轮子。
If you want far less bloat, consider ASP.NET MVC.如果您想减少膨胀,请考虑 ASP.NET MVC。 If I am guessing correctly, you will see the RTM at MIX, as Microsoft loves to release things at conferences.
如果我猜对了,你会在 MIX 上看到 RTM,因为微软喜欢在会议上发布东西。 If not, it is RC2, so RTM is not that far away.
如果不是,那就是RC2,所以RTM也不是那么遥远。 In ASP.NET MVC, the ASPX pages can be extremely thin, as they are just views.
在 ASP.NET MVC 中,ASPX 页面可以非常薄,因为它们只是视图。
If you want to tackle this your own way, you can do what you want with HTTP Handlers to handle the type.如果您想以自己的方式解决这个问题,您可以使用 HTTP 处理程序来处理类型。 I would not leave it as.htm or.html, as you will make it so you cannot serve a standard.html file on your server, which is not good.
我不会将其保留为 .htm 或 .html,因为您会这样做,因此您无法在服务器上提供 standard.html 文件,这不好。 This solves the handling issue, which is only half your battle.
这解决了处理问题,这只是你成功的一半。
I am not sure the best way to handle Intellisense.我不确定处理 Intellisense 的最佳方法。 One way is to reference, as shown on Stagner's blog: http://weblogs.asp.net/joestagner/archive/2008/05/12/add-custom-javascript-intellisense.aspx
一种方法是参考,如 Stagner 的博客所示: http://weblogs.asp.net/joestagner/archive/2008/05/12/add-custom-javascript-intellisense.aspx
I have not tried this with custom extensions, although it should work.尽管它应该可以工作,但我还没有尝试过使用自定义扩展。 You can also create your own custom XSD (XML) file, ala: http://vyasashutosh.blogspot.com/2007/05/providing-custom-intellisense-in-vsnet.html
您还可以创建自己的自定义 XSD (XML) 文件,例如: http://vyasashutosh.blogspot.com/2007/05/providing-custom-intellisense-in-vsnet.ZA2536FDC70D5FC78C
Personally, I would use the ASPX model and thin out what you don't need.就个人而言,我会使用 ASPX model 并减少你不需要的东西。 you can even whack the templates if you would like so you can do this with every page.
如果您愿意,您甚至可以修改模板,这样您就可以在每个页面上执行此操作。 You cannot get rid of the @ page directive, but if that is too much bloat, I am not sure ASP.NET is the model you wish to work with.
您无法摆脱 @ page 指令,但如果这太臃肿,我不确定 ASP.NET 是否是您希望使用的 model。
Not that i agree with you, but you can create protected variables on code behind and use them the way you are trying.并不是我同意你的观点,但你可以在后面的代码上创建受保护的变量,并按照你尝试的方式使用它们。
At code Behind:在代码后面:
protected string myVar1 = "Hello World"
At Html:在 Html:
<html>
<body>
<div><%= myVar1 %>
</body>
</html>
Again, I just want to make it clear that working this way is going one step back and I advise against it.再次,我只想明确表示,以这种方式工作会倒退一步,我建议不要这样做。
Try MVC framework .试试MVC 框架。
else use PHP, DJANGO, ROR or some other framework that supports templates fully.否则使用 PHP、DJANGO、ROR 或其他一些完全支持模板的框架。
Or try using spark for asp.net it attempts to remove the need for inline script tags by using xml markup IE或者尝试对 asp.net 使用 spark 它尝试通过使用 xml 标记 IE 来消除对内联脚本标签的需要
<% foreach( string key in collectionOfItems.Keys ) %>
<div><% =key %></div>
<% } %>
becomes something like变成类似的东西
<foreachcollectionOfItems.Keys >
<div>$key</div>
</foreach>
note my example is better crap and wrong but you get the idea.注意我的例子是更好的废话和错误,但你明白了。 google it... spark
谷歌它...火花
I believe if you inherit your class from the abstract UI.TemplateControl, you will get this functionality.我相信如果您从抽象 UI.TemplateControl 继承您的 class,您将获得此功能。 I'm browsing through the interfaces to see if simply implementing a specific interface will get this for you, but, I'm guessing if you want this functionality this is what you'll need to do.
我正在浏览接口,看看是否只需实现一个特定的接口就可以为您提供此功能,但是,我猜您是否需要此功能,这就是您需要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.