Код: Выделить всё
namespace PPISNetWebApi.Models
{
using System;
using System.Collections.Generic;
public partial class Attempt
{
public int AttemptId { get; set; }
public int DocumentJobId { get; set; }
public int LocationId { get; set; }
public int ServerId { get; set; }
public System.DateTime AttTime { get; set; }
public int AttResltId { get; set; }
public string Verified { get; set; }
public string Comment { get; set; }
public Nullable LocLat { get; set; }
public Nullable LocLon { get; set; }
public virtual AttReslt AttReslt { get; set; }
public virtual DocumentJob DocumentJob { get; set; }
public virtual Location Location { get; set; }
public virtual Server Server { get; set; }
}
}
< /code>
AttemptsController.csКод: Выделить всё
POSTКод: Выделить всё
[HttpPost]
[ResponseType(typeof(Attempt))]
public async Task PostAttempt(Attempt attempt)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Attempts.Add(attempt);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = attempt.AttemptId }, attempt);
}
< /code>
Maui app code - Attempt.csКод: Выделить всё
namespace PPISNetMobile.Models
{
public class Attempt
{
public int AttemptId { get; set; }
public int DocumentJobId { get; set; }
public int LocationId { get; set; }
public int ServerId { get; set; }
public DateTime AttTime { get; set; }
public int AttResltId { get; set; }
public string Verified { get; set; }
public string Comment { get; set; }
public Nullable LocLat { get; set; }
public Nullable LocLon { get; set; }
}
}
< /code>
AttemptServiceКод: Выделить всё
using PPISNetMobile.Models;
namespace PPISNetMobile.Services
{
public class AttemptService : IAttemptService
{
IRestService _restService;
public AttemptService(IRestService service)
{
_restService = service;
}
public Task GetAttemptsAsync()
{
return _restService.RefreshAttemptsDataAsync();
}
public Task SaveAttemptAsync(Attempt attempt, bool isNewItem )
{
return _restService.SaveAttemptAsync(attempt, isNewItem);
}
public Task DeleteAttemptAsync(Attempt attempt)
{
return _restService.DeleteAttemptAsync(attempt.AttemptId);
}
}
}
< /code>
IAttemptService.csКод: Выделить всё
using PPISNetMobile.Models;
namespace PPISNetMobile.Services
{
public interface IAttemptService
{
Task GetAttemptsAsync();
Task SaveAttemptAsync(Attempt attempt, bool isNewItem);
Task DeleteAttemptAsync(Attempt attempt);
}
}
< /code>
RestServiceКод: Выделить всё
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using PPISNetMobile.Models;
using PPISNetMobile.Services;
using PPISNetMobile.ViewModels;
namespace PPISNetMobile.Services
{
public class RestService : IRestService
{
HttpClient _client;
JsonSerializerOptions _serializerOptions;
IHttpsClientHandlerService _httpsClientHandlerService;
public List DocumentJobs { get; private set; }
public List Assignments { get; private set; }
public List AttReslts { get; private set; }
public List Attempts { get; private set; }
public List Locations { get; private set; }
public List Hair { get; private set; }
public List Races { get; private set; }
public List MobileViewModels { get; private set; }
public Task SelectedMobileViewModel { get; private set; }
public List ServiceInfos { get; private set; } = new List();
public List Relations { get; private set; }
public RestService(IHttpsClientHandlerService service)
{
#if DEBUG
_httpsClientHandlerService = service;
HttpMessageHandler handler = _httpsClientHandlerService.GetPlatformMessageHandler();
if (handler != null)
_client = new HttpClient(handler);
else
_client = new HttpClient();
#else
_client = new HttpClient();
#endif
_serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = null,
// WriteIndented = true
};
}
public async Task SaveAttemptAsync(Attempt attempt,bool isNewItem)
{
Uri uri = new Uri(string.Format(Constants.RestUrl3, string.Empty));
try
{
string json = JsonSerializer.Serialize(attempt, _serializerOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
if (isNewItem)
response = await _client.PostAsync(uri, content);
else
response = await _client.PutAsync(uri, content);
if (response.IsSuccessStatusCode)
Debug.WriteLine(@"\tAttempt successfully saved.");
}
catch (Exception ex)
{
Debug.WriteLine(@"\tERROR {0}", ex.Message);
}
}
}
}
< /code>
IRestService.csКод: Выделить всё
using PPISNetMobile.Models;
using PPISNetMobile.ViewModels;
namespace PPISNetMobile.Services
{
public interface IRestService
{
Task RefreshDataAsync();
Task RefreshAssignmentsDataAsync();
Task RefreshAttemptsDataAsync();
Task RefreshAttResltsDataAsync();
Task RefreshServiceInfosDataAsync();
Task RefreshMobileViewModelsDataAsync();
Task RefreshLocDataAsync();
Task RefreshHairDataAsync();
Task RefreshRacesDataAsync();
Task RefreshRelationsDataAsync();
Task SaveLocationAsync(Models.Location location, bool isNewItem);
Task SaveAttemptAsync(Attempt attempt, bool isNewItem);
Task AddServiceInfoAsync(ServiceInfo serviceInfo, bool isNewItem);
Task RefreshMobileViewModelDataAsync();
Task DeleteAttemptAsync(int id);
Task DeleteLocationAsync(int id);
Task DeleteServiceInfoAsync(int id);
}
}
< /code>
AttemptPage.xaml.csasync void OnSaveButtonClicked(object sender, EventArgs e)
{
int documentJobId = selectedMobileViewModel.DocumentJobId;
await _attemptService.SaveAttemptAsync(new Attempt
{
DocumentJobId = 15313,
LocationId = 16357,
ServerId = 27,
AttResltId = 4,
AttTime = DateTime.Now,
Verified = "verified",
Comment = "comment",
LocLon = (decimal?)1.11111,
LocLat = (decimal?)-2.22222
}, _isNewItem);
await Shell.Current.GoToAsync("///MobileViewPage");
}
< /code>
I am able to get attempts but not post an attempt. I keep getting Internal server error 500. Any help would be greatly appreciated!
Подробнее здесь: https://stackoverflow.com/questions/797 ... re-web-api
Мобильная версия