Does BlogEngine.Net have a custom HttpHandler for all .aspx requests? - asp.net

I'm working on customizing BlogEngine.Net to be able to return some HTML from an AJAX call. Basically I'd like to render a UserControl server-side and then return the resulting HTML to a client-side call.
I've done this many times in other applications using static PageMethods marked with the [WebMethod] attribute. But any time I try this with BlogEngine.Net, I get the full HTML of the page returned. It doesn't even look like the WebMethod is getting touched.
I've also tried to implement this as an HttpHandler, but I have the same result. As soon as I include a page (vanilla Page class) and use it to render the control, I get the full HTML of the page I am calling from instead of the generated code, leading me to assuming something is hijacking my code to render a Page.
Any ideas or alternate solutions to be able to render a user control server-side and return the HTML using the BlogEngine.Net framework?

If you look at the code of CommentView.ascx, they do the same thing using ICallbackEventHandler. That basically renders the comment preview and also the comment itself.
However, it should be possible to do it like you said too, with [WebMethod]. I have actually customized my own setup to change CommentView to use a [WebMethod] and it works fine.
For an example of their own [WebMethod] implementation, have a look at AjaxHelper under admin folder, it's a dummy page whose purpose is to serve these web methods.
The above are for BlogEngine.NET 2.6.

Related

Reusing ASP.NET code in multiple pages

Is there a way to reuse a piece of ASP.NET code (that tag-based code, not the code-behind) in many different pages? For example I have a <div> with some contents in it that appears in 5 different pages of my site. I'm looking for a technique through which I could save this <div> in a separate web-content file so to speak, with maybe a different extension like MyDiv.ASPC and could then do a server-side tag anywhere in a webpage like:
<asp:Import href="~/MyDiv.aspc" />
and ASP.NET would inject the entire content of this file at that point where this tag appears.
I thought of creating a user control for this, but I'm not sure if a user control always injects precisely what is written in its body, or could there sometimes be unwanted tags generated by user control itself.
Or are there existing better ways of doing this?
Edit
About Master Pages, they are far away from what I'm looking for. They are actually good for a common basic layout of your website. My idea is quite opposite of that. My pages do not have a common layout; it is just that they have one common <div>. It is more closely fulfilled by a UserControl.
For UCs, my fear is that they generate more than what is written in their body, whereas what I'm after is a simple text injection. To put it technically, what I'm looking for is basically a preprocessor step (kind of #include thing in C++) rather than a compiler step, if you see what I mean.
You need to use ASP.NET User Controls, as these are specifically created to be the solution to the problem you are describing. For more information, see MS Documentation.
From their documentation...
In addition to using Web server controls in your ASP.NET Web pages,
you can create your own custom, reusable controls using the same
techniques you use for creating ASP.NET Web pages. These controls are
called user controls.
A user control is a kind of composite control that works much like an
ASP.NET Web page—you can add existing Web server controls and markup
to a user control, and define properties and methods for the control.
You can then embed them in ASP.NET Web pages, where they act as a
unit.
An empty userControl would do just that - nothing. A user Control just adds it's contents to the page, or usercontrol hosting it. It adds nothing extra.
UserControls give you a nice easy page fragment type approach to reusing content. They work great within a project & most people use them for just that.
If you wanted to make something more reusable across projects, you could write server control. It's more involved, but much more reusable. Google should be able to find you many tutorials on how to do this.
Ran a short test. User Controls do not enter extra tags as long as you don't place any Runat="Server" tags in it, so this would indeed be a solution I guess.
You can also read output from a cache object where you would read your files.
So
<%= Static.ContentXyz %>
would mean:
public static class Static
{
public static string ContentXyz
{
get
{
string s;
if (!this.cacheDictionary.TryGetValue("ContentXyz", out s))
{
s = File.ReadAllText(Server.MapPath("ContentXyz.html"));
this.cacheDictionary("ContentXyz", s);
}
return s;
}
}
}

"WebMethod" attribute in the aspx and asmx files, difference?

I noticed in some code that [WebMethod] attribute is used in the code-behind file of an aspx page.
But as I remember it, this attribute is used to expose Web Service and it is often seen in asmx file.
So what's the difference between these 2 usages?
Thanks.
If that method is also static, you can call that method via javascript/ajax, without a full page postback.
Note that your ScriptManager needs to have the EnablePageMethods property set to true.
Webmethods in code behind are used for AJAX calls. If you are using jquery or similar and you need to implement an ajax functionality on your page then you will have to define you method with WebMethod attribute and have to make it public static. Then only it will work.
WebMethod's concept I feel has been taken from web services. Since asp.net didn't have any defined way of handling ajax requests to method of page behind, they have extended this feature to be used for code behind methods.
Keep a watch that being public static methods you may not be able to use your page class' internal properties here. so, you will need to deal with that.

aspx ashx mash-up

I'm retro-fitting a .aspx page with AJAX functionality (using VB, not C#). The codebehind populates the page with data pulled from a web-service. The page has two panels that are populted (with different data, of course) in this way. On a full page refresh, one or both panels might need to be populated. But populating Panel 2 can take a long time, and I need to be able to update panel 1 without refreshing Panel 2. Hence the need for AJAX (right?)
The solution I've come up with still has the old .aspx page with .aspx.vb codebehind, but introduces a Generic Handler (.ashx) page into the mix. Those first two components do the work on the user's first visit or on a full page refresh, but when AJAX is invoked, the request is handled by the .ashx page.
First question: Is this sound architecture? I haven't found a situation online quite like mine. Originally, I wanted to make the .aspx page into the AJAX handler by having the codebehind implement IHttpRequest, and then providing "ProcessRequest" and "IsReusable" methods, but I found I couldn't separate a regular visit to the page from an AJAX request, so my AJAX handlers took over even on the first visit to the page. Second question: Am I right to think that this approach (making the .aspx page do double-duty as the AJAX handler) will never work? Is it impossible to tell whether we're getting a full-page request or a partial-page (AJAX) request?
If the architecture is good, then I need to dynamically generate a lot of HTML in the .ashx file, right? If that is right, should I send HTML back to the client, or should I encode it in some way? I've heard of JSON encryption, but haven't figured out how to use it yet. So, Third question: Is "context.Response.Write" the only pipeline for sending data back to the client? And, if so, should I send back HTML or some kind of JSON-encoded objects?
Thanks in advance.
It sounds as if the page requires some AJAX functionality added to the UI.
Suggest using an UpdatePanel for each web form element that needs to have AJAXy refresh
functionality. That'll save you from having to refactor a bunch of code, and introduce a whole lot of HTML creation on your .ashx.
It'll be more maintainable over the long run, and require a shorter development cycle.
As pointed out by others, UpdatePanel would be a easier way - but you need to use multiple update panels with UpdateMode property set as conditional. Then you can trigger the update-panel refresh using any button on the page (see AsyncPostBackTrigger) or even using java-script (see this & this). On the server side, you may decide what has triggered the partial post-back and act accordingly by bypassing certain code if not needed.
You can also go with your approach - trick here is to capture the page output using HttpServerUtility.Execute in your ashx and write it back into the response (see this article where this trick has been used to capture user control output). Only limitation with this approach is that you can only simulate GET requests to your page and so you may have to change your page to accept parameters via query string. Personally, I will suggest that you create a user control that accept parameters via method/properties and will generate necessary output and then use the control on your page and in ashx (by dynmaically loading it in a temperory page - see this article).
EDIT: I am using jquery to illustrate how to do it from grid-row-view.
$(document).ready(function() {
$("tr.ajax-grid-row").click(function() {
$("#hidden-field-id").val($(this).find(".row-id").val()); // fill hidden filed
$("#hidden-button-id").click(); // simulate button click
});
});
You can place above script in the head element in markup - it is assuming that you have decorated each grid-row-view with css class "ajax-grid-row" and each row will have hidden field decorated with css class "row-id" to store row identifier or the value that you want to pass to server for that row. You can also use cell (but then you need to use innerHTML to get the value per row). "hidden-field-id" and "hidden-button-id" are client ids for hidden field and submit button - you should use Control.ClientID to get actual control ids if those are server controls.
JSON is not for that purpose, it is to pass objects serialized with a nice light weight notation, is you need to stream dinamically generated html using ashx, response.Write is what you have. You may want to take a look at MVC
Or you could use jquery if it's just html, the simpliest would be the load function, or you can look into Ajax with jquery. Since the ashx can be served as any resource it can be used in the load function.
I agree with #p.campbell and #R0MANARMY here. UpdatePanel could be the easiest approach here.
But then like me, if you don't want to go the UpdatePanel route, I don't see anything wrong with your approach. However, generating the html dynamically (entirely) at the back end is not a route I'll personally prefer (for the maintainence reasons). I'd rather prefer implementing a solution that will keep the design separate from the data.

Easiest way to simply display confirmation that a webservice worked?

I'm calling an asp.net webservice from an ASP clasic page basically just with:
<a href='http://domain/webservice.asmx/command'>Command</a>
and when users hit that button it works but they're just shown an xml page. The function will either work or not so I was wondering if it'd be possible to just have a pop up box appear to tell them if it worked or not after they clicked it rather than redirecting them to an xml page.
I'd prefer to not have to use jQuery or another javascript library.
If that's not possible, is there any way to dress up the XML page? Currently it says 'This XML file does not appear to have any style information associated with it. The document tree is shown below.' at the top.
Also, the domain that the webservice is on is different to the domain that the website that's call the webservice is on. Not sure if that matters.
Thanks
Check out this MSDN Link on Calling A WebService From Javascript Using AJAX. No JQuery is required and it boils down to having to use the ScriptService attribute on your WebService method and adding a ServiceReference in a ScriptManager control. You can then easily call your WebService from Javascript and it will call another Javascript function when it finishes. It is in that response function where you can add your confirmation display.
be aware that this is a bad idea to let the user handle directly - web services are almost always called by your code rather than a client browser session. One reason is that raw error information would be hown to the client if there were a problem.
If you really want to do this, you can either:
Use AJAX (No framework required - just JS) or
You can make the webservice non-standard so it returns user-friendly content - perhaps by wrapping it in a website which calls the API behind the scenes and formats the response in a meaningful fashion.

Put autocomplete methods in separate web file?

I have a web site with a growing number of AJAX calls.
I have AJAX code that looks like this:
function setupNameAutocomplete(id) {
$(id).autocomplete({
source: function(request, response) {
$.ajax({
url: "selectName.aspx/getNameAutocomplete",
....
Originally, the above javascript code was used on a web page where a user typed in the name of a person to search for; I decided I wanted to have autocomplete on the name.
However, as I continued to develop the web site, I decided that I wanted to be able to use the same "name" autocomplete on many pages. Logically, it made sense to put my javascript code in an external .js file instead of repeating it on every page.
It also makes sense that I would want to put the .net code that handles the AJAX in its own file as well. The .net autocomplete code looks like this:
[WebMethod]
public static IEnumerable<string> getNameAutocomplete(string text)
{
IEnumerable<string> values = lookupNamesThatStartWith(text);
return values;
}
Naturally, it seemed like this codes belongs in an external .asmx or perhaps a .ashx file, but I can't get my javascript code working unless I call the above code from selectName.aspx.
How can I get my AJAX .net code in a separate code file?
If I understand your problem correctly, a viable solution would be to have a base class that your services inherit from that would implement the generic WebMethod.
That, or you could get it "pretty clean" by having the logic of the autocomplete lookup abstracted into a class and just call it from your WebMethods each time, as needed.

Resources