Web Api Help Page - all <see cref="MyClass"/> are missing - asp.net-web-api

In my source code documentation I often use:
Get a <see cref="Quote"/> for the specified <see cref="apiOrder"/> object.
And this translates nicely into the below string in the XmlDocument.xml, which contains the compiled web api help pages.
Get a <see cref="T:Supertext.API.POCO.Quote"/> for the specified <see cref="!:apiOrder"/> object.
But for some reasons, all these references are not being displayed.
What we get is this:
Get a for the specified object
We found a few sources, but nothing seems to work. Does not help:
Web Api Help Page- don't escape html in xml documentation
Outdated:
http://thesoftwaredudeblog.wordpress.com/2014/01/04/using-microsoft-asp-net-web-api-2-help-page-part-2/
Any ideas?

In the WebAPI 2 Help, there is a class called, XmlDocumentationProvider. In this class there is a method named, GetTagValue which handles the Summary and Returns tags. There is also a method named GetDocumentation (there are multiples, but it is the one with the HttpParameterDescriptor parameter) which handles the Param tags.
I wrote a function that uses a RegEx to find all "See Cref"s and replace them with the last object name found.
The RegEx:
private static Regex SeeCodeReferenceRegEx = new Regex("<see cref=\\\"\\w:([\\w]+\\.)*(\\w+)\\\" */>", RegexOptions.Compiled);
The function:
private static string CleanValue(string value)
{
value = value.Trim();
var matches = SeeCodeReferenceRegEx.Matches(value);
foreach (Match match in matches)
value = value.Replace(match.Groups[0].Value, match.Groups[2].Value);
return value;
}
In GetTagValue, replace:
return node.Value.Trim();
with:
return CleanValue(node.InnerXml);
In GetDocumentation replace:
return parameterNode.Value.Trim();
with:
return CleanValue(parameterNode.InnerXml);

Related

MvvmCross - navigation with custom objects

I have followed the steps in this link
Passing complex navigation parameters with MvvmCross ShowViewModel
i implemented an instance of the IMvxJsonConverter, and registered it. this is my code for my view model
public class AccountDetailsViewModel : BaseViewModel<AccountDetailsNav>
{
private readonly Repository.AccountsRepository _accounts;
Account _fullAccount;
public AccountDetailsViewModel(Repository.AccountsRepository accounts)
{
_accounts = accounts;
}
protected override void RealInit(AccountDetailsNav parameter)
{
//stuff
}
I have tried simple types by just passing thru strings , this is the code i use to navigate to to the viewmodel
Mvx.RegisterSingleton<Repository.AccountsRepository>(() =>
{
return _accounts;
});
ShowViewModel<AccountDetailsViewModel>(nav);
But it never ever seems to arrive in my view model methods or populates my data, and i cannot for the life of me figure out why. the data is serialized fine , and i have even tried blank constructors to no avail .. i just cannot figure out why its not hitting the realinit
K i found the problem , when adding a new view i failed to remove this method on the code behind of the view, and as such was causing my viewmodel to be null and never hitting my breakpoints
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}

WebApi Help Page: don't escape HTML in XML documentation

I am using XML Documentation for my ASP.NET Web API Help Page as shown here.
I would like to know if there is a way to include html in the comments such that it will be rendered on the web page, instead of it being removed/ignored/escaped.
Specifically, I am looking for a way to create a newline, but being able to create bulleted lists, etc. would be great!
Ex/ I would like to be able to do something like this:
/// <summary>
/// CRUD operations for SalesDocument<br/>
/// This is a new line
/// </summary>
[RoutePrefix("api/SalesDocument")]
public partial class SalesDocumentController : ApiController
And have it show on the help page like this:
CRUD operations for SalesDocument
This is a new line.
Instead of this: (in this case, <br/> gets removed somehow - if I try using <p> tags, they are just escaped)
CRUD operations for SalesDocument This is a new line.
*I have already tried the <para> tag as suggested by multiple posts for tooltips, but this does not work on my help page.
Any suggestions are greatly appreciated!
In the installed XmlDocumentationProvider.cs file at Areas\HelpPage, you can look for a method called GetTagValue. Here modify the return value from node.Value.Trim() to node.InnerXml.
private static string GetTagValue(XPathNavigator parentNode, string tagName)
{
if (parentNode != null)
{
XPathNavigator node = parentNode.SelectSingleNode(tagName);
if (node != null)
{
return node.InnerXml;
}
}
return null;
}
Now open the installed file Areas\HelpPage\Views\Help\DisplayTemplates\ApiGroup.cshtml and modify the following line from:
<p>#controllerDocumentation</p>
to
<p>#Html.Raw(controllerDocumentation)</p>
#controllerDocumentation does not work for me, but changing the line to#api.Documentation works. i.e. #html.raw(api.Documentation).

Using Routes to correct typos in a URL

I'm managing an MVC3 app where I need to support the ability of 3rd parties to create link to assets within my domain. Because some of the links are sliced and diced by mail merges and other text editing problems, URLs with typos have been introduced, e.g.:
/Content/ima!+ges/email/spacer.gif
or
/Content/image++s/email+/spacer.gif
I'd like to strip these extraneous characters by RegEx before attempting to serve them. I _think this is something a Route method could accomplish and I'd welcome a pointer or two to articles that demonstrate this approach.
ADDENDUM (cuz I need the formatting):
Implementing #Nathan's routing I'm unable to send the filename to the controller handler - it's always seeing a null value passed in. I've tried both 'filepath' and 'path' with the same 'null' result.
routes.MapRoute(
"MangledFilename",
"{*filepath}",
new { controller = "MangledFilename", action = "ServeFile" }
);
I think this is a matter of configuring wildcard handling on IISExpress and am looking for that solution separately. The more serious immediate problem is how your suggestion returns the HttpNotFound - i'm getting a hard IIS exception (execution halts with a YellowScreenDeath) instead of the silent 404 result.
public ActionResult ServeFile(string filePath)
{
if (filePath != null) // workaround the null
{
...
}
return HttpNotFound();
}
thx
I think something along this approach should work:
First add a route like this to the end of your route registering declarations:
routes.MapRoute(
"MangledFilename",
"{*filepath}",
new { controller = "MangledFilename", action = "ServeFile" });
If you haven't seen them before, a route parameter with an * after the opening { is a wildcard parameter, in this case it will match the entire path. You could also write it like content/{*filepath} if you wanted to restrict this behavior to your content directory.
And then a controller something like this should do the trick:
public class MangledFilenameController : Controller
{
public ActionResult ServeFile(string filePath)
{
filePath = CleanFilePath(filePath);
var absolutePath = Server.MapPath(filePath);
if (System.IO.File.Exists(absolutePath))
{
var extension = System.IO.Path.GetExtension(absolutePath);
var contentType = GetContentTypeForExtenion(extension);
return File(absolutePath, contentType);
}
return HttpNotFound();
}
private string CleanFilePath(string filepath)
{
//clean the path up
return filepath;
}
private string GetContentTypeForExtenion(string extension)
{
//you will want code here to map extensions to content types
return "image/gif";
}
}
In regards to mapping an extension to a MIME / content type for the GetContentTypeForExtension method, you could choose to hard code types you are expecting to serve, or use one of the solutions detailed in this post:
File extensions and MIME Types in .NET
EDIT:
After thinking about it, I realized there's another way you can handle the ServeFile action. Redirecting to the existing file could be simpler. I'm leaving the original method I wrote above and adding the alternative one here:
public ActionResult ServeFile(string filePath)
{
filePath = CleanFilePath(filePath);
var absolutePath = Server.MapPath(filePath);
if (System.IO.File.Exists(absolutePath))
{
return RedirectPermanent(filePath);
}
return HttpNotFound();
}
I believe #Nathan Anderson provided a good answer but it seems incomplete.
If you want to correct the typos and the types are as simple as those you mentioned then you can use Nathan code but before trying to find the file, you remove any plus or exclamation point characters in the path and you can do it like this:
String sourcestring = "source string to match with pattern";
String matchpattern = #"[+!]";
String replacementpattern = #"";
Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern));
Generated this code from the My Regex Tester tool.
This is the code you need. This code also removes any + character from the filename. If you don't want that behavior, you may select a substring without the filename and only replace + and ! characters before the filename.

Return current URL of a List in itemAdding Event Receiver?

In an ItemAdding Event Receiver I want to return the full URL of the current List using c#. How do I do this?
I have tried:
string currentURL = SPContext.Current.File.Url;
This however returns "Object reference not set to an instance of an object" at runtime.
Basically looking for guidance on what I need to do to return the current URL in this Event Receiver?
Thanks
You get the "Object reference not set to an instance of an object" error because SPContext.Current is null.Try this instead:
string url = properties.Web.Url + "/" + properties.ListItem.Url;
Some note about SPContext.Current - you CAN use SPContext in your
EventReceiver, but you must save it in your event receiver's
construstor, something like this public MyItemReceiver() { current =
SPContext.Current; } and than use this saved context.

How does "see" work in the method docs?

/// <summary>
/// Get all following siblings of each element up to but not including the element matched by the selector.
/// </summary>
/// <param name="selector">A string containing a selector expression to indicate where to stop matching following sibling elements.</param>
/// <see cref="http://api.jquery.com/nextUntil/"/>
/// <returns></returns>
public SharpQuery NextUntil(string selector = null)
{
throw new NotImplementedException();
}
I wanted to add a link in my method docs to link to a fuller explanation. "see" seemed appropriate for this (intellisense suggested it). However, when I call start typing my method, "see" doesn't appear in the tooltip. Is there a way to go to that link? I tried pressing F1, it took me to MSDN instead.
The <see> tag must be used within the text of other comment tags in order to specify a hyperlink.
You can also use <seealso> to specify a hyperlink to appear in a See Also section of the generated documentation.
MSDN provides the following example:
/// text for class TestClass
public class TestClass
{
/// <summary>DoWork is a method in the TestClass class.
/// <para>Here's how you could make a second paragraph in a description. <see cref="System.Console.WriteLine(System.String)"/> for information about output statements.</para>
/// <seealso cref="TestClass.Main"/>
/// </summary>
public static void DoWork(int Int1)
{
}
/// text for Main
static void Main()
{
}
}
From what I gathered at:
http://msdn.microsoft.com/en-us/library/5ast78ax(VS.80).aspx
This tags ( , ) will be available in the generated documentation file (the XML file, when you do /doc compiler options), and then further processed by tool like Sandcastle

Resources