[英]showing json from Web API controller on Razor view
I am working on an asp.net MVC 5 application. 我正在开发一个asp.net MVC 5应用程序。 I created a Web API 2 controller which returns list of medicines. 我创建了一个Web API 2控制器,该控制器返回药品列表。 Now I want to display them to view. 现在,我要显示它们以供查看。 I created another controller named homecontroller and index action method ( because I think api controller can not have action method). 我创建了另一个名为homecontroller和index action method的控制器(因为我认为api控制器不能具有action方法)。 I created index view. 我创建了索引视图。 In that view I want to show that list of medicines. 在这种情况下,我想显示药品清单。 I followed this link http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api 我点击了此链接http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
I am trying this code: 我正在尝试此代码:
var uri = '/api/Medicines';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#products'));
});
});
});
but getting 404 in browser console. 但在浏览器控制台中获取404。
This is the Web APi controller method: 这是Web APi控制器方法:
public class MedicinesController : ApiController
{
public IEnumerable<Medicine> GetMedicines()
{
Random rnd = new Random();
return medicines.OrderBy(x => rnd.Next()).ToList();
}
}
Route config is: 路由配置为:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Please suggest how to fix this. 请提出解决方法。 I need list of medicines in json format. 我需要json格式的药品清单。
Thanks 谢谢
Link '/api/Medicines' will work if route is configured similar to 如果将路由配置为类似于,则链接“ / api / Medicines”将起作用
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
But if you have it like this (there both versions in tutorial) 但是,如果您具有这样的功能(教程中同时有两个版本)
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
you have to also specify Action name in uri '/api/Medicines/Medicines'. 您还必须在uri'/ api / Medicines / Medicines'中指定操作名称。
I've created new MVC 5 project with WebApi and everithing worked without any problems and hacks. 我已经使用WebApi创建了新的MVC 5项目,并且一切正常,没有任何问题和黑客。 You cant try to compare with your solution - test repo on github . 您不能尝试与github上的解决方案- 测试存储库进行比较。
If you have stadard route configuration and not use any areas etc your uri should be 如果您具有标准路线配置并且未使用任何区域等,则您的uri应该是
var uri = '/Medicines/GetMedicines'
And as Joanvo said you migth be need allowGet setting in return. 正如Joanvo所说,您的迁移需要获得allowGet设置的回报。 Like this: 像这样:
return Json(medicines.OrderBy(x => rnd.Next()), JsonRequestBehavior.AllowGet);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.