Posts
1147
Comments
890
Trackbacks
1
July 2016 Blog Posts
Fixing ImageLink code that Asp.NET Core broke

This code, prior to Asp.NET Core, worked to create an image link:

public static HtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
       {
           UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
           TagBuilder imgTag = new TagBuilder("img");
           imgTag.MergeAttribute("src", imgSrc);
           imgTag.MergeAttribute("alt", alt);
           imgTag.MergeAttributes((new RouteValueDictionary(imgHtmlAttributes)), true);
           string url = urlHelper.Action(actionName, controllerName, routeValues);

           TagBuilder imglink = new TagBuilder("a");
           imglink.MergeAttribute("href", url);
           imglink.InnerHtml = imgTag.ToString(TagRenderMode.SelfClosing);
           imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

           return new HtmlString(imglink.ToString());
       }

 

It’s usage was:

@Html.ImageLink(Url.Content("~/content/images/product/4/" + Model.Image), Model.Name, “Detail”, “Product”, new { id = Model.ID }, null, null)

Now, given that Asp.NET Core is a huge re-write, one should expect the APIs to change.  What one might not expect is that:

imgTag.ToString()

Would return the fully qualified class name, as opposed to, you know, the string representation of the content of the TagBuilder.

Why did they choose to do something this stupid (and it was a choice, this is explicitly by design)?  Who knows?  After a long day dealing with deliberately obstinate people, I don’t care.

This fixes it:

public static HtmlString ImageLink(this IHtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
        {
            ActionContext ac = new ActionContext(htmlHelper.ViewContext.HttpContext, htmlHelper.ViewContext.RouteData, htmlHelper.ViewContext.ActionDescriptor);
            UrlHelper urlHelper = new UrlHelper(ac);
            TagBuilder imgTag = new TagBuilder("img");
            imgTag.TagRenderMode = TagRenderMode.SelfClosing;
            imgTag.MergeAttribute("src", imgSrc);
            imgTag.MergeAttribute("alt", alt);
            imgTag.MergeAttributes((new RouteValueDictionary(imgHtmlAttributes)), true);
            string url = urlHelper.Action(actionName, controllerName, routeValues);

            TagBuilder imglink = new TagBuilder("a");
            imglink.MergeAttribute("href", url);
            imglink.InnerHtml.Append(GetString(imgTag));
            imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

            var imgLinkDecoded = System.Web.HttpUtility.HtmlDecode(GetString(imglink));

            return result = new HtmlString(imgLinkDecoded);        }

        public static string GetString(IHtmlContent content)
        {
            var writer = new System.IO.StringWriter();
            content.WriteTo(writer, HtmlEncoder.Default);
            return writer.ToString();
        }

Note the brilliant code that requires you to decode a string that was HtmlEncoded (since you cannot WriteTo() something that doesn’t HtmlEncode it).

Brilliant.

posted @ Wednesday, July 27, 2016 9:41 PM | Feedback (0)
The MSDN subscription people and processes need some work

So I have a Visual Studio MSDN Enterprise subscription which I’ve had since 2009 (back when it was Ultimate), which I originally subscribed to under a particular email account.

A year or two ago, I changed the Microsoft account that it was listed under/connected to/ whatever.  I don’t recall exactly, but it seems like it went fine.

This year, after I renewed it, it expired.  I called in to point out the problem and they created a ticket, and the person online noted that I had in fact paid and they did something in some database somewhere, and it was reflected in their system but would take 2 or 3 days to be reflected online.

Fast forward through 6 weeks as I spoke with 2-3 different people to deal with the same issue.  To be fair, they would often call me and say “do you have access now?” and when I told them I didn’t, they said they would do something in some database somewhere and I should see it in 2-3 days.  The last person asked for confirmation through email that I had actually renewed in the first place, and then finally, I had access.

So, I asked for my expiration date to be forwarded to reflect the 2 months I didn’t have access.

They now call me every couple of days to ask if I have access, to which I reply that I do, but my expiration date has not been updated, to which they reply that they are escalating to their subscription manager, etc.

At least they call me.  I should feel loved.

posted @ Thursday, July 14, 2016 1:09 PM | Feedback (0)
Self-care for snowflakes

If you find life so very very hard, this may contain good advice for you.

posted @ Thursday, July 14, 2016 9:56 AM | Feedback (0)
Web Template Fail: Visual Studio Enterprise 15 Preview 3

Create a new project based on the .NET 5 web template.

Try to build.

1>C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.9\tsc.exe  --noEmitOnError --locale en-US --listEmittedFiles
1>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\TypeScript\Microsoft.TypeScript.targets(261,7): error MSB4064: The "OutputLogFile" parameter is not supported by the "VsTsc" task. Verify the parameter exists on the task, and it is a settable public instance property.
1>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\TypeScript\Microsoft.TypeScript.targets(247,5): error MSB4063: The "VsTsc" task could not be initialized with its input parameters.
1>Done building project "EntPrev3-WebTemplate.xproj" -- FAILED.

Do they even look at the work they are doing?

posted @ Monday, July 11, 2016 11:03 AM | Feedback (2)
What is Dona Sarkar looking at?

Just curious.  People keep using this image.

posted @ Wednesday, July 06, 2016 12:31 PM | Feedback (0)
There’s levels of irony here

The man who’s made a career of making videos about things he doesn’t really know is writing a book.

I’m sure it will be a good read.

posted @ Monday, July 04, 2016 8:01 PM | Feedback (0)
Workaround: Failed to make the following project runnable: Asp.NET Core 1.0

This isn’t great by any stretch, but this works for me on the 1 of 3 machines that I have this issue:

Check out the entire solution for edit.

Delete the \bin and \obj folders from the web site.

You should now be able to build at will.

You’ll need to repeat whenever you check something in.

I know, this sucks.

posted @ Sunday, July 03, 2016 5:55 PM | Feedback (3)
Asp.NET Core 1.0 and TFS, works on my machine, as long as it’s just the one machine and not the other

Can build and run just fine on my Surface Pro 4, but not on my Surface Book, which fails to build with the previously mentioned error.

Let’s hear it for the Scotts, well done guys.

posted @ Friday, July 01, 2016 9:47 AM | Feedback (0)