我正在构建一个简单的费用跟踪系统。我在本地运行它时代码工作正常,但当我尝试连接到Azure中的端点时,我得到404错误。我甚至试图完全删除移动服务并重新发布它,但它只能在我的本地机器上运行。
spoofy@spoofers:~$ curl -v https://mxpense.azure-mobile.net/tables/Expense
* About to connect() to mxpense.azure-mobile.net port 443 (#0)
* Trying 191.236.80.12... connected
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-SHA
* Server certificate:
* subject: C=US; ST=WA; L=Redmond; O=Microsoft; OU=OrganizationName; CN=*.azurewebsites.net
* start date: 2014-03-24 21:01:22 GMT
* expire date: 2016-03-23 21:01:22 GMT
* subjectAltName: mxpense.azure-mobile.net matched
* issuer: DC=com; DC=microsoft; DC=corp; DC=redmond; CN=MSIT Machine Auth CA 2
* SSL certificate verify ok.
> GET /tables/Expense HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: mxpense.azure-mobile.net
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Length: 0
< Server: Microsoft-IIS/8.0
< X-Powered-By: ASP.NET
< Set-Cookie: ARRAffinity=11de5874c445a15c401ea654cfeb62ad8ed363d5c21bc48e2b33658554d05d9b;Path=/;Domain=mxpense.azure-mobile.net
< Date: Fri, 01 Aug 2014 00:12:03 GMT
<
* Connection #0 to host mxpense.azure-mobile.net left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
我使用Azure SQL DB中的一些示例数据为数据库播种,因此我知道数据存在
当然是我的种子方法
List<Expense> expenses = new List<Expense>{ new Expense { Id = System.Guid.NewGuid().ToString(), Amount = 7.77, Client = "Cisco", Location = "Houston,tx", Date = DateTime.UtcNow},
new Expense {Id = System.Guid.NewGuid().ToString(),Amount = 10.99, Client= "Microsoft", Location = "Remond, WA", Date = DateTime.UtcNow}};
foreach (Expense exp in expenses)
{
context.Set<Expense>().Add(exp);
}
base.Seed(context);
对象的类def
public class Expense: EntityData
{
public double Amount { get; set; }
public ExpenseCurrency? Currency { get; set; }
public DateTime? Date { get; set; }
public string Vender { get; set; }
public ExpenseType? Type { get; set; }
public string Location { get; set; }
public string Client { get; set; }
public string Project { get; set; }
public bool? PersonalExpense { get; set; }
public string UserId { get; set; }
public int? CompanyId { get; set; }
}
更新
似乎Azure移动服务SDK正在请求错误的路径。它不是请求http:// {service name} .azure-mobile.net / api / Expenses,而是请求https:// {service name} .azure-mobile.net / tables / Expenses
这是init
private MobileServiceCollection<Expense, Expense> expenseCollection;
private IMobileServiceTable<Expense> expenseTable = App.MobileService.GetTable<Expense>();
这里是引发异常的地方
expenseCollection = await expenseTable.ToCollectionAsync();
更新2
请求了feecontroller类..
public class ExpensesController : ApiController
{
private mXpenseContext db = new mXpenseContext();
// GET: api/Expenses
public IQueryable<Expense> GetExpenses()
{
return db.Expenses;
}
// GET: api/Expenses/5
[ResponseType(typeof(Expense))]
public async Task<IHttpActionResult> GetExpense(string id)
{
Expense expense = await db.Expenses.FindAsync(id);
if (expense == null)
{
return NotFound();
}
return Ok(expense);
}
// PUT: api/Expenses/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutExpense(string id, Expense expense)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != expense.Id)
{
return BadRequest();
}
db.Entry(expense).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ExpenseExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Expenses
[ResponseType(typeof(Expense))]
public async Task<IHttpActionResult> PostExpense(Expense expense)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Expenses.Add(expense);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (ExpenseExists(expense.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = expense.Id }, expense);
}
// DELETE: api/Expenses/5
[ResponseType(typeof(Expense))]
public async Task<IHttpActionResult> DeleteExpense(string id)
{
Expense expense = await db.Expenses.FindAsync(id);
if (expense == null)
{
return NotFound();
}
db.Expenses.Remove(expense);
await db.SaveChangesAsync();
return Ok(expense);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool ExpenseExists(string id)
{
return db.Expenses.Count(e => e.Id == id) > 0;
}
}
答案 0 :(得分:1)
默认情况下,移动服务(.Net)中的控制器继承自TableController,如您的错误消息所示,其中它寻找到/ tables / expenses的路由。由于您的控制器继承自ApiController(完全可以按照webApi),您应该能够添加路由属性以获得所需的结果。
// GET: api/Expenses
[Route("api")]
public IQueryable<Expense> GetExpenses()
{
return db.Expenses;
}
// GET: api/Expenses/5
[Route("api/{id}"]
[ResponseType(typeof(Expense))]
public async Task<IHttpActionResult> GetExpense(string id)
{
Expense expense = await db.Expenses.FindAsync(id);
if (expense == null)
{
return NotFound();
}
return Ok(expense);
}
答案 1 :(得分:0)
似乎问题出在我的控制器上。它继承自ApiController,但将其切换到TableController解决了这个问题。当脚手架确保使用'windows mobile services table controller'选项时
答案 2 :(得分:0)
您的控制器应显示在帮助页面下,您可以在本地和托管时查看。如果托管,则使用您的主密钥作为密码(用户名并不重要)。
TableControllers连接在/ tables / *路径下,而所有其他控制器连接在/ api / *下。对于TableControllers,您可以使用客户端表API,对于所有其他控制器,您可以使用自定义API。
谢谢,
的Henrik