How to convert asmx service to rest to invoke with URL - c#

I have created asmx service, how do I invoke it via "URL"?
Here is my code:
[WebMethod]
public string start(string id, string name)
{
string newname = name;
return newname;
}
Here is my current URL:
url = "http://localhost:XXXXX/WebService.asmx/start?id=" +id+ "&name="+name;
Here is my "Web.config":
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5">
</compilation>
<httpRuntime maxRequestLength="1000000"/>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

You can decorate your method to allow HTTP GET requests
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public string start(string id, string name)
{
string newname = name;
return newname;
}
and do the following in web.config file
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
Setting the UseHttpGet property to true might pose a security risk for
your application if you are working with sensitive data or
transactions. In GET requests, the message is encoded by the browser
into the URL and is therefore an easier target for tampering.
Note that this method of performing GET requests does come with some
security risks. According to the MSDN documentation for UseHttpGet:
Hope this helps

Related

WCF can't create endpoint

I have a library with some service in a separate project DuplicateServiceLibrary.
public class DuplicateService: IDuplicateService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
I created a host with a following svc file:
<%#ServiceHost Service="DuplicateServiceLibrary.DuplicateService" %>
and config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
I am trying to create an endpoint:
Adding server
When I click on DuplicateServiceLibrary I et the following error:
Inside the dll
The expected result should be a list of services containing 1 item. What is wrong?
The error description is in Polish. English caption would read "Could not load file or assembly". There is also an error code to identify it.

WS should return only JSON not XML wrapped JSON

I have a WS which should return only JSON, however its being wrapped in a XML.
Already removed the
[WebService(Namespace = "http://www.url.com/")]
But it has no affect, my WS looks like this
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class pointer : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld(string name)
{
Account account = new Account
{
Email = "james#example.com",
Active = true,
CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
Roles = new List<string>
{
"User",
"Admin"
}
};
return JsonConvert.SerializeObject(account, Formatting.Indented);
//return "Hello World";
}
the return looks like this:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://tempuri.org/">
{ "Email": "james#example.com", "Active": true, "CreatedDate": "2013-01-20T00:00:00Z", "Roles": [ "User", "Admin" ] }
</string>
my config looks like this:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="500000000"/>
</webServices>
</scripting>
</system.web.extensions>
<system.webServer>
<handlers>
<add name="ScriptHandlerFactory"
verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
resourceType="Unspecified" />
</handlers>
</system.webServer>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0" />
</system.web>
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="pointer.asmx" />
<add value="default.aspx" />
</files>
</defaultDocument>
<security>
<authorization>
<add accessType="Allow" users="?" />
</authorization>
</security>
<httpErrors errorMode="Detailed" />
<!--<handlers accessPolicy="Read, Execute, Script" />-->
</system.webServer>
Is there anyway to keep the wrappers away and just return JSON?
The ScriptService automatically serialize the result to JSON, you dont have to do it manually, otherwise it gets serialized twice. I think the content-type from the client is specifying application/xml instead of application/json?

Glimpse works locally, not on remote server

After following all the guides, SO pages and troubleshooting pages I can find I'm finally out of ideas.
I've got Glimpse working fine on my local dev server, but when I deploy my ASP.net (MVC5) app to my remote server it doesn't work - at all. /glimpse.axd gives a 404 with both LocalPolicy and ControlCookiePolicy set to ignore, and with a custom security policy that returns On in all cases. My understanding is that with ControlCookiePolicy disabled, I shouldn't need to go to /glimpse.axd to enable it - but I'm not seeing the glimpse icon on the remote server either.
Even if I go to the remote server and browse localhost to /glimpse.axd I still get a 404.
My web.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="glimpse" type="Glimpse.Core.Configuration.Section, Glimpse.Core" />
</configSections>
<system.web>
<compilation debug="false" />
<httpRuntime targetFramework="4.5.1" relaxedUrlToFileSystemMapping="true" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication" />
</modules>
<urlCompression doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
</system.webServer>
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<logging level="Trace" />
<runtimePolicies>
<ignoredTypes>
<add type="Glimpse.AspNet.Policy.LocalPolicy, Glimpse.AspNet" />
<add type="Glimpse.Core.Policy.ControlCookiePolicy, Glimpse.Core" />
</ignoredTypes>
</runtimePolicies>
</glimpse>
</configuration>
This is the version off the remote server (after transform). I've trimmed it a little to remove sections like appSettings.
My GlimpseSecurityPolicy.cs looks like this:
// Uncomment this class to provide custom runtime policy for Glimpse
using Glimpse.AspNet.Extensions;
using Glimpse.Core.Extensibility;
namespace RationalVote
{
public class GlimpseSecurityPolicy:IRuntimePolicy
{
public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
{
return RuntimePolicy.On;
}
public RuntimeEvent ExecuteOn
{
// The RuntimeEvent.ExecuteResource is only needed in case you create a security policy
// Have a look at http://blog.getglimpse.com/2013/12/09/protect-glimpse-axd-with-your-custom-runtime-policy/ for more details
get { return RuntimeEvent.EndRequest | RuntimeEvent.ExecuteResource; }
}
}
}
The real one does an actual check, but I get the same issue with the policy above.
I cannot seem to find any trace output anywhere on the remote server, it is logging fine on my local machine.
I am deploying using the Visual Studio publish to web feature, and I've verified that the Glimpse.Core.dll is in the bin folder.
I can't see anything in the event log that is relevant.
I've also added <add namespace="Glimpse.Mvc.Html" /> to the namespaces block of the web.config in the views folder.
I tried putting #Html.GlimpseClient() in the _Layout.cshtml file just above </body> but this renders nothing.
Anybody got any ideas?
If the glimpse.axd is returning a 404 then this means the Glimpse resource handler is not registered.
If the web.config content you show above is not trimmed to much, then it is normal that Glimpse won't do much as the Glimpse HttpModule and the Glimpse HttpHandler are not registered in the system.web and/or the system.webserver sections like this
<system.web>
<httpModules>
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet"/>
</httpModules>
<httpHandlers>
<add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet"/>
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode"/>
</modules>
<handlers>
<add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />
</handlers>
</system.webServer>
Maybe your transform removed to much from the local web.config?

Asp.net Routing wrong MIME type for .css files served with UrlRoutingModule and PageBuildProvider

I need to route request like:
http://www.site.com/channel1/somestring
http://www.site.com/channel1/otherstring
http://www.site.com/channel2/anotherstring
to my default page: http://www.site.com/default.aspx page
for a web project that runs on .net 4.5, IIS Express with integrated pipeline.
Routes:
protected virtual void RegisterRoutes()
{
RouteTable.Routes.MapPageRoute("AliasCss","{channel}/css/{*url}","~/css/{url}",
false,null, new RouteValueDictionary { { "channel", "(channel1|channel2)"}});
RouteTable.Routes.Ignore("{resource}.js");
RouteTable.Routes.Ignore("{resource}.css");
RouteTable.Routes.MapPageRoute("Alias", "{*url}", "~/Default.aspx", true,
new RouteValueDictionary { { "url", "Default.aspx" } },
new RouteValueDictionary { { "url", "(channel1|channel2)" } });
}
It works great except for the css files
Example: http://www.site.com/channel1/css/custom.css
that gives 404 error insted of serving the css files in http://www.site.com/css/custom.css
To solve this I needed to add a PageBuildProvider for making Asp.net serve static .css files and set runAllManagedModulesForAllRequests="true"
web.config relevant parts:
<configuration>
...
<system.web>
<compilation debug="true" targetFramework="4.5" >
<buildProviders>
<add extension=".css" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
<httpRuntime targetFramework="4.5" enableVersionHeader="false" />
...
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="true" />
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
...
</system.webServer>
...
</configuration>
Now .css files are correctly routed but are served with wrong MIME type:
Resource interpreted as Stylesheet but transferred with MIME type
text/html
I tried to set the mimetype in web.config but with no luck:
<staticContent>
<remove fileExtension=".css" />
<mimeMap fileExtension=".css" mimeType="text/css; charset=UTF-8" />
</staticContent>

Doesn`t run http handler in asp.net application

I want to run my httphandler, when some user get access to image file with extentions like png, jpeg, gif and other. But i get eror 404 when i try to path. I think what server try find file on a disk, but i want use alias for access to file in my handler and newer access for real phisical file path.
Example config file:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpHandlers>
<add verb="GET,HEAD" path="*.jpg" type="Startup.Shop.ImageHandler, Startup.Shop" validate="false"/>
</httpHandlers>
<authorization>
<allow users="*" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="ImageHandlerfor JPG" path="*.jpg" verb="GET" type="Startup.Shop.ImageHandler, Startup.Shop" resourceType="Unspecified" />
<add name="ImageHandler for GIF" path="*.gif" verb="GET" type="Startup.Shop.ImageHandler, Startup.Shop" resourceType="Unspecified" />
<add name="ImageHandler for BMP" path="*.bmp" verb="GET" type="Startup.Shop.ImageHandler, Startup.Shop" resourceType="Unspecified" />
<add name="ImageHandler for PNG" path="*.png" verb="GET" type="Startup.Shop.ImageHandler, Startup.Shop" resourceType="Unspecified" />
</handlers>
</system.webServer>
/configuration>
Example handler class:
public class ImageHandler : IHttpHandler, IRouteHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return this;
}
}
And more - iis server configurated as classic mode
If you're in classic mode, then asp.net won't get called by IIS unless IIS knows the file type that should get mapped. In your IIS control panel, you should be able to configure the file types handled by ASP.NET. Otherwise, it's going to assume that a file type is for a physical file, which it can't find on the file system.

Categories

Resources