1

Let's say I have 2 MVC web projects (web1 and web2) and 1 project containing shared views (common) (using the razorgenerator of David Ebbo)
web1 and web2 both have a test.cshtml file. Several blocks of code in both test.cshtml files are exactly the same.
I'm trying to find out if it's possible to share a declarative helper (@helper) between several cshtml files which are in DIFFERENT projects. So putting a cshtml file in my App_Code does not help (I would need to have 1 in each web project, which is obviously not what I want).
I know I could create a bunch of shared partial views in my 'common' project, but it seems kinda overhead to create 20 cshtml files that each contains a very small portion of html.
I know I can create a standard helper method (static string GenerateAPieceOfHtml(this HtmlHelper helper, ....)), but there I loose the ease of writing html as you can do it in a cshtml file.

For a short while I thought I bumped into an answer that would allow me to do it. But as I wrote in a comment, that code did not compile for me.

I hope my question is clear :)

[Update]
As csharpsi asks in a comment.. I did try out the code from the other post, but it did not spit out any HTML for me. Since I started to think that that answer should probably do the trick since it has 13 upvotes, I decided to give it a second try..
Again I didn't get any output, but then I tried it a little bit different.. and success!
I was trying this (which doesn't spit out any html on the page):

@{ new Test().DoSomething(Model); }

This is the version that DOES WORK:

@{
  var html = new Test().DoSomething(Model);
  @html
}  

Other version that works:

@(new Test().DoSomething(Model))

What should I do with this question? Delete it? Write an answer myself?

Community
  • 1
  • 1
TweeZz
  • 4,779
  • 5
  • 39
  • 53
  • Did you try the code from the other post without declaring the `HelperResult` as static? – Simon Oct 18 '11 at 11:04
  • I'd say: answer that by yourself, citing the user that put you on the right track with his/her comment – superjos Jun 25 '13 at 18:24

1 Answers1

0

Why are you trying to use razor helper for this anyway ? Razor helpers are one-particular-viewengine hack, your application shouldnt rely on them on many places (even amongst different websites). In this case, be sure to use standard MVC way - HTML helper. These you can easily share between websites, for example you can make your own class library full of them.

rouen
  • 5,003
  • 2
  • 25
  • 48
  • As I wrote in my question: I know I can use standard HTML helpers for that. I do think though that the possibility to use razor syntax is much easier then concatenate a bunch of strings in a helper method. So I don't really see this as an answer on my question, because you didn't answer my question (is it possible to...) and you write that I should use a way which I know is possible and would like to avoid :) – TweeZz Oct 18 '11 at 11:32
  • I didnt try to answer your question, i tried to convince you that you "are doing it wrong" :) You are moving part of the shared logic into the engine-specific sytax sugar. Imagine you will accomplish that, use that extensively, and then your stupid boss come and say "oh, i hate that razor, switch it to webforms/spark/whatever engine immediately" - if you would use only recommended MVC stuff, you can do it in a minute (even with conversion tool), but with your hack you are screwed... see my point ? dont break practices - you will bring shit upon you sooner or later :) – rouen Oct 18 '11 at 21:29
  • Sry, no offense, but no, I don't see your point. My boss has no clue what razor, webforms, sparks, ... is. My boss needs to see results and he does not care which view engine we use. I'm even not sure if he _should_ care. "shared logic"? There is hardly any logic in the (duplicated) parts I want to move out and if there is, it's the same in all views. It's simple html. "use only recommended MVC stuff".. Is what I'm trying to achieve against any rules or recommendations? I'm simply trying to refactor out duplicated blocks of html. – TweeZz Oct 19 '11 at 05:27
  • And if you don't try to answer a question, then maybe you should just use a comment instead of an "answer" :) – TweeZz Oct 19 '11 at 05:28