在响应JSON的ASP.NET ASMX WebMethod中,我是否可以抛出异常并设置HTTP响应代码?我想如果我抛出一个HttpException,状态代码将被适当地设置,但它不能使服务响应除500错误之外的任何东西.
我尝试过以下方法:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void TestWebMethod() {
throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message");
}
Run Code Online (Sandbox Code Playgroud)
也:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void TestWebMethod() {
try {
throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message");
}
catch ( HttpException ex ) {
Context.Response.StatusCode = ex.GetHttpCode();
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
这些都回应500.
非常感谢.
我正在尝试了解web api和一些有关Web方法的新闻.我听说我们应该停止使用几种来源的网络方法.另外,如果不再使用Web方法,Web API是继承者吗?
我一直在努力让我的jquery调用webmethod工作.我被服务器以"401 Unauthorized"响应退回.我必须在web.config或其他阻止成功调用的地方设置不正确.
非常感谢您的见解!
button.OnClickAction = "PageMethod('TestWithParams', ['a', 'value', 'b', 2], 'AjaxSucceeded', 'AjaxFailed'); return false;";
Run Code Online (Sandbox Code Playgroud)
function PageMethod(fn, paramArray, successFn, errorFn) {
var pagePath = window.location.pathname;
var urlPath = pagePath + "/" + fn;
//Create list of parameters in the form:
//{"paramName1":"paramValue1","paramName2":"paramValue2"}
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + …Run Code Online (Sandbox Code Playgroud) asp.net authentication jquery webmethod http-status-code-401
我想弄明白,但现在没有成功.是否可以在webmethod asmx服务中使用async/await?到目前为止我发现async/await只能在WCF服务方法中使用(休息或其他).
我熟悉web方法.现在我有一个使用Web API而不是web方法的建议.我已经完成了ASP.NET Web API的演示,它使用传统的asp.net web开发更接近MVC架构.我不喜欢用经典开发搞砸控制器(MVC概念).
我的网站方法:
[WebMethod]
public static string GetName(int id)
{
return "testName";
}
Run Code Online (Sandbox Code Playgroud)
我的Web API控制器:
public class MyController : ApiController
{
[HttpGet]
public string GetName(int id)
{
return "testName";
}
}
Run Code Online (Sandbox Code Playgroud)
我真的很困惑这个问题,任何一个人都有更好的想法.
对于同样哪个更好的选择,你有什么建议?
如果两个代码都相同,我怎么比较?
我正在使用Glass开发使用Glassfish的Web服务.我有几个Web方法,我想将我的webmethod名称及其参数介绍给http head请求.
例如:
我有这条道路:
context:WebServices
webMethod:makeSomething
参数:a = 2
所以我创建了一个名为ProfilingFilter的类:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, javax.servlet.ServletException {
if (request.getContentLength() != -1 && context != null) {
((HttpServletResponse) response).addHeader("Operation", -->PATH+PARAMETERS);
// ((HttpServletResponse) response).addHeader("Operation", -->makeSomething?a=2);
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用servlet响应或servlet请求来获取此信息?
如果没有,我该怎么做?
我正在使用jqPlot根据Web方法的数据生成堆积条形图.
图表成功呈现,但为空白.当我将pointLabels设置为'true'时,它们会出现在图表左侧的混乱中.我猜测堆叠的条形图也是在图表外渲染,但我不明白为什么.
有人可以解释一下如何解决这个问题吗?
这是web方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<dataPoint> getPartnerOrderVolumes()
{
List<dataPoint> p = new List<dataPoint>();
DataTable dt = new DataTable();
chart jep = new chart(5);
foreach (chartData cd in jep.lstChartData)
{
dt = cd.GetData();
}
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
dataPoint dp = new dataPoint();
dp.x1Value = row[2].ToString();
dp.y1Value = row[3].ToString();
dp.y2Value = row[4].ToString();
p.Add(dp);
}
}
return p;
}
Run Code Online (Sandbox Code Playgroud)
这是web方法使用的dataPoint类:
public class dataPoint
{
public string x1Value { get; set; } …Run Code Online (Sandbox Code Playgroud) 在.NET中,是否有标准方法来指示Web服务方法已被弃用?
为了澄清,通过'web服务方法',我的意思是一个用[WebMethod]属性装饰的方法.
标准做法只是使用[Obsolete]属性将其标记为已弃用,就像任何其他方法一样?
我在我的站点中的所有页面上都有一个函数,它位于我的母版页中,我希望它从一些jQuery Ajax方法运行.
我现在有一些像这样的代码
jQuery的
$(document).ready(function() {
$("#test").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#test").text(msg.d);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
主页面中的HTML
<div id="test">Click here for the time.</div>
Run Code Online (Sandbox Code Playgroud)
我的Master VB背后的Asp.Net代码
<WebMethod> _
Public Shared Function GetDate() As String
Return DateTime.Now.ToString()
End Function
Run Code Online (Sandbox Code Playgroud)
目前,除非我将Web方法移动到Default.aspxVB页面,否则这不起作用
有没有办法可以改变这一部分
url: "Default.aspx/GetDate",
Run Code Online (Sandbox Code Playgroud)
要使用母版页功能?
我试着改成它
url: "template.master/GetDate",
Run Code Online (Sandbox Code Playgroud)
但这只是给我一个404错误
有任何想法吗?
提前致谢
我有一个带有简单Web服务的ASP.NET 4.0 WebForms页面WebMethod.此方法用作异步/ TPL代码的同步包装器.我面临的问题是,内部Task有时会有一个null SynchronizationContext(我的偏好),但有时会有一个同步上下文System.Web.LegacyAspNetSynchronizationContext.在我提供的示例中,这并不会导致问题,但在我的实际开发场景中可能会导致死锁.
对服务的第一次调用似乎总是使用空同步上下文运行,接下来的几个也可能.但是一些快速激活的请求会开始弹出ASP.NET同步上下文.
[WebMethod]
public static string MyWebMethod(string name)
{
var rnd = new Random();
int eventId = rnd.Next();
TaskHolder holder = new TaskHolder(eventId);
System.Diagnostics.Debug.WriteLine("Event Id: {0}. Web method thread Id: {1}",
eventId,
Thread.CurrentThread.ManagedThreadId);
var taskResult = Task.Factory.StartNew(
function: () => holder.SampleTask().Result,
creationOptions: TaskCreationOptions.None,
cancellationToken: System.Threading.CancellationToken.None,
scheduler: TaskScheduler.Default)
.Result;
return "Hello " + name + ", result is " + taskResult;
}
Run Code Online (Sandbox Code Playgroud)
存在的定义TaskHolder:
public class TaskHolder …Run Code Online (Sandbox Code Playgroud) asp.net synchronizationcontext task-parallel-library webmethod async-await
webmethod ×10
asp.net ×5
asmx ×4
.net ×3
async-await ×2
jquery ×2
web-services ×2
ajax ×1
architecture ×1
asynchronous ×1
charts ×1
deprecated ×1
java ×1
javascript ×1
jqplot ×1
servlets ×1