Invalid Webmethod name - c#

I created a web method but when i go to the site and type for example http://mysite.com/services/Amounts/GetAmount it returns an error Internal Server Error 500. After investigating the issue in event logs etc,, it says GetAmount invalid method name. but i know the mame is fine
[WebMethod(EnableSession=true)]
public string GetAmount(Amounts amts)
{
//some logic here to add to the database.
}
What are the possible issues that I have to look into when this type of error shows?, I checked all the references and everything is named properly "GetAmount".

What are you trying to do here? You need to post more code and web.config. Which version of .NET you are using here? All this information may get you the better answer.
Firstly, your URL http://example.org/services/Amounts/GetAmount does not seems to be correct - there has to be .asmx somewhere unless you are using ASP.NET routing or some url rewriting.
Assuming that your routing/re-writing is indeed working correctly:
in general, if its a normal SOAP Web Service then enable HTTP get -
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
If you are trying create a service callable from script (ScriptService) then for asmx service, you need to mark the method as ScriptService and you may need to adjust web.config based on your .NET version. Also you need to enable HTTP GET - for example,
[ScriptMethod(UseHttpGet = true)]
public string GetAmount(Amounts amts)
You may also need to adjust Response Format whether you want JSON or XML.

Related

Receiving "potentially dangerous Request.Form" with requestValidationMode = 2.0

I'm trying to pass raw HTML using ViewBag and Html.Raw, I have another instance of logic identical to this that works in my app but when I duplicated it on another page I'm getting this error again.
I have already enabled <httpRuntime requestValidationMode="2.0" /> in my config file. Why am I still getting this message? Shouldn't this allow it across the entire application?
Give [ValidateInput(false)] in your method.

ScriptMethod in ASPX page, called from browser, IIS6 Returning 404

I have code which works on the dev environment (my machine),
though doesn't work on IIS6
I have a method in an ASPX page which has a footprint similar to this:
[WebMethod()]
[ScriptMethod()]
public static string HelloWorld(string name)
{
return 'Hi '+name;
}
When I look at the console on the browser, I can see my script call this method, though IIS returns a 404 not found.
The script does a http POST to this url:
http://mydomain.com/myPage.aspx/HelloWorld
I am guessing it has something to do with mime types on IIS?
Found the solution to this,
The problem was caused by two things.
Firstly, I needed to add this to the Web.Config
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
Then , the other issue was the use of a tool for url rewriting called UrlRewritingNet
(http://www.urlrewriting.net/149/en/home.html)
One of the ways to configure this module, is to put in in IIS6 as an ISAPI filter , matching a wild card * , so if no file extension was matched, this filter would run the query.
The way I got around the second problem was to do an XML query to my service, and not a json one. Which then uses a url with a file extension when making the call.
Hope this helps

How to check Authentication Mode value in Web.Config without referencing System.Web

I have a class that needs to check the Authentication Mode from a web.config.
Ex:
<authentication mode="Forms" />
or
<authentication mode="Windows" />
Now, I know this can be done pretty easily with the following code:
AuthenticationSection sec = ConfigurationManager.GetSection("system.web/authentication");
if (sec.Mode == "Windows")
{ ... }
My problem is, this class/project is being referenced in my Web project, as well as a WinForms project. The WinForms project is requiring .NET 4.0 Client Profile Framework (we don't want to require the full .NET 4 Framework, if possible). If I'm not mistaken, the Client Profile does not contain System.Web.dll.
Is there a way that this value can be checked without referencing System.Web (and preferably without manually parsing the config file)?
I've tried:
object authSection = ConfigurationManager.GetSection("system.web/authentication");
if (authSection.ToString() == "Windows")
{ ... }
However the ToString() simply returns the string "System.Web.Configuration.AuthenticationSection".
Thank you!
I have used the above code to get the authentication mode. I just done few changes in your code. Please find here.
AuthenticationSection authSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
if (authSection.Mode.ToString() == "Windows")
Hey if your talking about a web config in the same project try using the following method.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel)
Or you could use one of the other similar methods in the ConfigurationManager members. I can't test it for you at the moment but I'm pretty sure they should work. Because essentially they don't care what kind of conf file it is as long as there is one as since the inherited type of the web.config is a config, you should be able to access it just like any other and query for the particular field you need.
ConfigurationManager
Where in your code do you need to make a decision on this? If the user is authenticated at that point you could use IIdentity.AuthenticationType and process accordingly. For Forms this will always return Forms, for a Windows identity it typically NTLM, although it can be Negotiate or Kerberos.

How to increase the POST size for an ASMX web service?

Background
I am developing an ASP.Net server side control that needs to talk to an ASMX web service. The server side control uses a WebClient object to talk to the web service, since it needs to be reused often in various application, and to make it easier on the developers, they are not required to create a service reference to the web service.
Implementation
During the use of the control, it is requires the sending of a serialised object to the web service. The object is serialised using the XmlSerializer and the resulting XML string is then compressed using the chilkat compression library. The web service call for the control looks as follows:
webClient.UploadStringAsync(new Uri(serviceHost + serviceMethod), "POST", sendData)
The content of sendData (string) is compressedResponse={CompressedData}.
The web service has a method defined as follows to receive the data and then decompress the string value using the chilkat library before de-serialising the object using the XmlSerializer.
public void SaveResponse(string compressedResponse)
The communication between the control and the service is working. Initially there were no settings or binding defined in the web.config for any of the above. After initial searching I did add
<httpRuntime maxRequestLength="20480"/>
to both the client and server web.config files. This has made no difference.
Problem
Compressed or uncompressed the data being posted to the web service in the sendData variable is to big for a normal POST request, and is corrupted. This is confirmed when checking the last few characters of the string before and after it being posted to the server in compressed format, and uncompressed, the Xml document is missing the last root tag when checking in the debugger. The string can't be decompressed and therefore the service call fails every time.
How do I increase the POST size for the WebClient request to ensure that the full string is received by the server?
I have looked at the various option on Google, but none are giving me a good enough sample of where to make the changes, or samples of what the changes need to look like. I am completely lost as to whether the change needs to be made on the server or the consuming website, and since there are no binding defined for this, how to create a binding in the web.config for an ASMX HTTP service call.
I believe you must be hitting ASP.NET max request length limit. That you can modify via config file such as:
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
maxRequestLength value is in KB, so above setting would allow 20 MB. You can also apply the setting only to selected URLs using location tag e.g.
<location path="yourservice.asmx">
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
</location>
There seems to be no way to change the POST size for a ASMX Web Service when only HttpPost is enabled.
The solution in the end was to switch the service to running HttpSoap and create a service reference to the assembly containing the control. Once done the binding is created using code in the control once the endpoint is set via a property.

HttpHandler and XML files

I would like to intercept any request made to the server for XML files. I thought that it might be possible with an HttpHandler. It's coded and it works... on localhost only (?!?!).
So, why is it working on localhost only? Here is my web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.xml" type="FooBar.XmlHandler, FooBar" />
</httpHandlers>
</system.web>
</configuration>
Here is my C# :
namespace FooBar
{
public class XmlHandler : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
Response.Write(xmlString);
}
}
}
As you might have seen, I'm writing the xmlString directly in the response, it's only temporary because I'm still wondering how I could give the filename instead (that's the second question ;) )
What is supposed to be written in the response is only the xml filename that will be retrieved by a flash app.
Thanks
Details :
Using IIS 6.0 on Windows Server 2003.
Edit :
When calling the page from another computer it looks like it's not getting to the HttpHandler. However, the mapping for IIS have been done correctly.
I don't have an IIS6 server at hand at the moment, but there are two steps required:
map the xml extension to ASP.NET (use the executable path from aspx extension): Setting Application Mappings in IIS 6.0
tell ASP.NET to use your custom handler: Deploying HTTP Handlers and HTTP Modules
The first step is not obvious because the Visual Studio integrated web server is mapping all requests to ASP.NET.
Other resources:
How to: Create Synchronous HTTP Handlers
if IIS is version 6.0 or previous,
handler will be ignored, because IIS handle xml extensions without calling ASP.NET process.
you can change it from IIS Manager,saying iis to use asp.net for handling XML.

Categories

Resources