Generally in my company we use MS C# Coding Conventions for C# code (.cs files). So, typically, our code looks more or less like this:
public class FooController
{
public ActionResult Index()
{
for (int i = 0; i < 10; i++)
{
// Code here
}
if (true == false)
{
// More code here
}
return View();
}
}
Which looks nice and readable.
But in my team, the designers (who usually work only on Razor cshtml files) use a different formatting in their code. More or less like this:
#using (Html.BeginForm()) {
#if (true == false) {
<p>You'll never see this!</p>
}
}
They generally open curly braces in the same line and have other formatting options because they only use cshtml (Razor) files. (They do other tricks as well so the source code looks similar to generated HTML code.)
We have all agreed on that and it's cool.
The problem starts when I or some other developer edits part of the Razor view and Visual Studio re-formats the code using our (C#/cs file) settings. This is not desirable.
The question is:
Is there a way to have different formatting options for .cs and .cshtml files, or to stop Visual Studio from formatting Razor cshtml files specifically?
Any help will be kindly appreciated.
Visual Studio does not have such options.
I'm using Telerik's JustCode. It has different formatting settings for C# and Razor. It also has some other useful for developers stuff.
Related
I am .Net developer and am looking for example related to creation of dynamic navigation menu in umbraco 7 using .Net user control without using MVC technique and XSLT. I have searched it on google but got not much response on it, examples which i got were using either XSLT or Razor. I have not any experience in MVC and XSLT and looking for technique related to using classic ASP.Net, i.e. without using XSLT and MVC. It will a great help if any one could please provide some useful videos or links with examples which i could refer to.
Thanks
Tarunjit Singh
You should really look into the Razor thing. It just TOO simple not to try!
It looks like c# mixed with some HTML. If you really want to stick to .Net UserControls you might miss out on future versions.
Creating navigation in a usercontrol is very difficult because you have 2 options
you need to write out HTML yourself
create some custom model to some 3th party control
Both of these options will never return the exact html as you wanted. Razor (and xslt) on the other hand will.
If you still want try something in a .Net User control anyway, get an instance of the Umbraco Nodes you want. Let's give an example:
using System;
using System.Linq;
using Umbraco.Web;
public partial class UserControls_TestUserControl : UmbracoUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var cache = this.UmbracoContext.ContentCache;
var rootNodes = cache.GetAtRoot().First().Children;
foreach (var node in rootNodes)
{
Response.Write("<li>" + node.Name + "</li>");
}
}
}
If you want to call this code, don't forget to add a macro, and insert the macro in your template.
Just in case you would like to explore razor, here is an equivalent:
#inherits UmbracoTemplatePage
#{
var homePage = CurrentPage.AncestorOrSelf(1);
var menuItems = homePage.Children;
}
<nav>
<ul>
<li>Home</li>
#foreach (var item in menuItems)
{
<li>#item.Name</li>
}
</ul>
</nav>
This example can be used as a partial view. And can be put in your (Razor) template using: #Html.Partial("NameOfThePartial")
You have to admit, it's not too hard.
Is there an easy way to convert the output of Excel 2003's Save to HTML option to something more 'friendly'? I have an Excel document with about 20 columns and 50 rows.
Throughout the saved HTML file I have obsolete HTML (example: <table x:str border=0 cellpadding=0 cellspacing=0 width=1366 style='border-collapse:
collapse;table-layout:fixed;width:1025pt'>), in line styles, and empty cells all over the place.
I'd like something less 'table-y'. Is there a way to either force Excel 2003 to save it using more CSS and less tables or a way to convert tables to divs more easily than going through the HTML file by hand?
If still of interest, Tidy shall neatly do the task in hand!
It seems to be somewhat without support so expect to find some troubles to make it useful for your needs.
Anyway, for POC you can rely on a on-line version found at http://infohound.net/tidy/.
Just checking the 'Clean' option leaving everything as-is would lead to a result close to your expectations.
Regards and good luck.
If you have Dreamweaver, you can use its markup cleaner tool...which I believe is designed for this kind of thing.
Create a new HTML document in Dreamweaver, copy and paste your code in and use the cleanup tool.
Here's one guide I find, but you could probably find better if it doesn't help:
http://www.articleonlinedirectory.com/656381/cleaning-up-unwanted-formatting-dreamweaver.html
Good luck.
Open the html file in browser and run some code in console is a way, a rough example:
var whiteList = ["rowspan", "colspan"];
[...document.querySelectorAll("table")].forEach(table => {
rmAttr(table);
[...table.querySelectorAll("tr")].forEach(tr => {
rmAttr(tr);
[...tr.querySelectorAll("td")].forEach(td => {
rmAttr(td);
});
});
console.log(table.outerHTML);
});
function rmAttr(dom) {
[...dom.attributes].forEach(attr => {
if (!~whiteList.indexOf(attr.name)) {
dom.removeAttribute(attr.name);
}
});
}
In short, with a RichHtmlField we can customize which Styles & Markup Styles appear in the drop down lists on the Ribbon. Using an OOTB Content Editor Web Part we cannot.
After interrogating / contrasting the RichHtmlField vs. CEWP (HTML/JavaScript emitted) I was indeed able to find a JavaScript solution.
While the RichHtmlField emits a Page Script to initialize the field control, the CEWP emits no such initialization script. Moreover, the RichHtmlField emits a significantly greater number of HTML Attributes than the CEWP does, and two of those attributes are PrefixStyleSheet and StyleSheet.
PrefixStyleSheet identifies the style sheet prefix being used in the drop down menu.
StyleSheet is a server relative url to the branded css file.
So, with all these things being true, we should be able to initialize a CEWP with a branded stylesheet using jQuery:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(function() {
// window.setTimeout(function() {
$("div[RteRedirect]").each(function() {
var id = $(this).attr("RteRedirect"),
editSettings = $("#" + id);
if(editSettings.length > 0 && editSettings[0].PrefixStyleSheet != 'yourcustom-rte') {
editSettings[0]['PrefixStyleSheet'] = 'yourcustom-rte';
editSettings[0]['StyleSheet'] = '\u002fStyle Library\u002fen-US\u002fThemable\u002fCore Styles\u002f<somecustomstylesheet>.css';
RTE.Canvas.fixRegion(id, false);
}
});
// }, 2000);
}, "sp.ribbon.js");
</script>
I hope this helps someone. This code could go a lot of different directions. I'm merely presenting the fundamentals. If you add this to a master page inside an EditModePanel it will work cross site.
There is a timer in there as depending on the order of script loading something may mess with what you've done after you've done it. Let me know if this works / doesn't work for you.
Would be cool to keep a running tally of solutions to this problem somewhere.
Cheers,
M
believe it or not 4 years late this post was helpfull !
To make it work I had to change this :
$("td[RteRedirect]").each(function() {
var id = $(this).attr("RteRedirect");
editSettings = $("#" + id);
Thank you
Brief description of the default behavior i want to override:
Creating a SharePoint list and using the "Title (linked to Item)" in a view provides a link to the DispForm for that list. By default, this link appends a Source parameter to the URL query string.
I would like to disable this behavior in some way while still retaining the default list view web part that is created by SharePoint when the view is defined.
To be clear, I know how to convert the list-view webpart to XSLT and then control this behavior. If at all possible, I'd like to keep from doing that so that the list view can still be easily supported by Help Desk and also keep the benefits of the standard list view (such as exporting to excel and so forth).
Any ideas?
Thanks in advance for your help.
Troy
SharePoint (2007) dynamically appends the source parameter in a javascript method named GoToLink. That method is defined in SharePoint's core.js file, and it can be easily customized without editing the original file. In fact, you never want to edit that file directly; it will put SharePoint into an "unsupported" state.
We'll create a solution for our customization, so it can easily be deployed or retracted in a consistent manner. Create a new Empty SharePoint solution in Visual Studio, targeted for GAC deployment. Within your project, create the following folder structure:
Templates
Layouts
1033
("1033" is for the English localization; if you're using a different language, your id will be different).
Within "1033", create a new Javascript file (we call our ours "CustomCore.js"). This will contain JUST the code we want to override from Core.js. Since GoToLink is the only thing we're interested in, let's focus on that. Here's the original from Core.js:
function GoToLink(elm) {
if (elm.href==null)
return;
var ch=elm.href.indexOf("?") >=0 ? "&" : "?";
var srcUrl=GetSource();
if (srcUrl !=null && srcUrl !="")
srcUrl=ch+"Source="+srcUrl;
var targetUrl=elm.href+srcUrl;
if (isPortalTemplatePage(targetUrl))
window.top.location=STSPageUrlValidation(targetUrl);
else
window.location=STSPageUrlValidation(targetUrl);
}
Not much to it. For our override, we just need to omit the bits that add the source parameter. From my reckoning, the modified method looks like this:
function GoToLink(elm) {
if (elm.href==null)
return;
if (isPortalTemplatePage(elm.href))
window.top.location=STSPageUrlValidation(elm.href);
else
window.location=STSPageUrlValidation(elm.href);
}
That should be it. Package up the solution and Visual Studio should interpret the "Templates" folder structure correctly when it builds the WSP (look in the generated manifest file for the TemplateFile element and that it's deploying to "Layouts\1033\CustomCore.js").
Once your WSP solution is deployed to SharePoint, we have the final step of referencing it in your Master page. In the HEAD section of your master page, you should see Core.js being referenced like this:
<SharePoint:ScriptLink Language="javascript" runat="server" Defer="True" Name="core.js"/>
We simply add a reference to the new JS file directly beneath this line:
<SharePoint:ScriptLink Language="javascript" runat="server" Defer="True" Name="core.js"/>
<SharePoint:ScriptLink Language="javascript" runat="server" Defer="True" Name="customcore.js"/>
Test it out and make sure it works. Note that this will affect ALL lists on the server to which you've deployed "CustomCore.js" and which use the master page.
In asp.net webpages framework what is the difference between using a content block versus a helper?
They both seem to be used to output HTML to multiple pages. They both can contain code and both can pass parameters.
Are there other differences? When should you use a helper versus a content block?
More info:
With Content Blocks we create a .cshtml (for example _MakeNote.cshtml) file to hold the content we want to insert into a page. Then we use:
#RenderPage("/Shared/_MakeNote.cshtml")
to insert the content into a page. We can pass parameters to the content block like this:
#RenderPage("/Shared/_MakeNote.cshtml", new { content = "hello from content block" })
It's somewhat like an include file, but I think does not share scope with the parent page.
With Helpers we create a .cshtml page in the App_Code folder (for example MyHelpers.cshtml) and place methods in that page which we want to call. The method looks something like this:
#helper MakeNote(string content) {
<div>#content</div>
}
The helper is called by using:
#MyHelpers.MakeNote("Hello from helper")
There isn't a lot of difference functionally. Helpers need to go into an App_Code folder - unless you download VWD or Visual C# Express and compile a binary - and the App_Code folder doesn't translate well to the MVC framework. Of course, that's only relevant if you want to upgrade to MVC at some point.
I would use a helper for generic functional snippets like your MakeNote. I would use a "content-block" (partial, really) for repeated site-specific sections of a page.