0

Is it possible in ASP.NET MVC 5 to dynamically create a partial view (cshtml) in the /Views/Shared directory? I have a situation where people are going to upload a bunch of HTML as strings and was thinking it would be better for performance to store them on the file system.

Is it as simple as creating a new file, steaming a string and saving?

Scott Dietrich
  • 686
  • 1
  • 8
  • 20

3 Answers3

1

Yes it is possible

Simply make a view like DynamicView.cshtml

@model DynamicView
@Html.Raw(Model.HTMLString)

Now the method you will use to store both the HTML and the pointer to it is a different story. You can either store the sanitized HTML in a data-base and retrieve it with a call to the Controller like

public ActionResult DynamicView(ind id)
{
DynamicView model = new DynamicView();
DynamicView.HTMLString = dbContext.HTMLViews.Where(v => v.id == id);

return View(model);
}

If you wish to write the submitted HTML to files instead, you can instead do

public ActionResult DynamicView(string filePath)
{
DynamicView model = new DynamicView();
DynamicView.HTMLString = ...code that reads file

return View(model);
}

See this related post Writing/outputting HTML strings unescaped

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • Looks like you have the reading of the info covered. I'm looking to avoid usage of the DB. I'm concerned about writing files to the file system. It seems that writing HTML might be straight forward, but writing CSHTML might involve some hurdles (like compiled views vs non-compiled). I guess if I'm just writing HTML chuncks it should be as easy as System.IO.File. – Scott Dietrich May 22 '15 at 21:08
0

Quick Answer: No (you cannot/should not modify Views Folder at RunTime.)

ASP.NET MVC Views and Shared Views are meant to be compiled and run. Modifying them or Adding new ones as the Application is running is not at all advisable or practical.

What would make more sense is for you to store the uploaded html blobs in a database, file system, storage blob (cloud). Then code your Shared View or Specific Views to retrieve specific html blobs from the stored location depending upon which user is logged in.

There are a whole lot of Extension Functions in MVC that Enable you to insert partial html in views. take a look at PartialExtensions as an starting point.

Ehsan Samani
  • 130
  • 6
  • The OP is asking if it is 'possible'. You are only saying what is advisable. – AmmarCSE May 22 '15 at 21:05
  • @AmmarCSE The OP Asked whether you can dynamically (while the application is running) perform IO into Views Folder. despite the negative votes, I will reiterate. Modifying Views Folder, web.config or anything of sort that can trigger asp.net recompilation is not at all wise. Asp.net MVC emphasizes convention. It is not the convention to mix arbitrary user provided html blobs with released code. the solution you provided also avoids modifying the Views Folder at run-time as well. so I am not sure where your disagreement stems from. – Ehsan Samani May 22 '15 at 21:41
  • sorry, I misunderstood your answer. Btw, I cannot undo my downvote because it is telling me it is locked until you edit your answer. Can you do a quick edit so I can undo my downvote? – AmmarCSE May 22 '15 at 21:43
  • Thanks. I have clarified (qualified) my answer. – Ehsan Samani May 22 '15 at 22:03
0

I had a requirement to create top menu which will generate from DB. I tried a lot to create it using partial view. But i found most of the cases partial view is use for static content. at last i fond some helpful tutorial please find the following i hope it will help you too.

click here to see how to create Dynamic Partial view

Sapnandu
  • 620
  • 7
  • 9