Hi User, to change the Directory and Name of the SwaggerClient.cs you have to open the .csproj file and add the Arguments ClassName="BikeStoreClient" OutputPath="..\Models\BikeStoreClient.cs".
For me it works better to Download the swagger.json file and add the Service Reference as a file.
After this you should adjust your Controller a bit, because it might get generated wrong:
here's mine:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebApplication3.Controllers
{
public class StoreController : Controller
{
BikeStoreClient client;
public StoreController()
{
client = new BikeStoreClient("http://localhost:35519", new HttpClient());
}
private T AwaitTask<T>(Task<T> t)
{
t.Wait();
return t.Result;
}
private void AwaitTask(Task t)
{
t.Wait();
}
// GET: StoreController
public ActionResult Index()
{
return View(AwaitTask(client.GetStoresAsync()));
}
// GET: StoreController/Details/5
public ActionResult Details(int id)
{
return View(AwaitTask(client.GetStoreFromIDAsync(id)));
}
// GET: StoreController/Create
public ActionResult Create()
{
return View();
}
// POST: StoreController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind("City,Email,Phone,State,StoreName,Street,ZipCode")]Store store)
{
try
{
if (ModelState.IsValid)
{
AwaitTask(client.PostStoreAsync(store));
return RedirectToAction(nameof(Index));
}
}
catch(Exception e)
{
ModelState.AddModelError("", e.Message);
}
return View(store);
}
// GET: StoreController/Edit/5
public ActionResult Edit(int id)
{
return View(AwaitTask(client.GetStoreFromIDAsync(id)));
}
// POST: StoreController/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, [Bind("City,Email,Phone,State,StoreName,Street,ZipCode")] Store store)
{
if (id != store.StoreId)
{
return NotFound();
}
try
{
if (ModelState.IsValid)
{
AwaitTask(client.PutStoreAsync(id, store));
return RedirectToAction(nameof(Index));
}
}
catch(Exception e)
{
ModelState.AddModelError("", e.Message);
}
return View();
}
// GET: StoreController/Delete/5
public ActionResult Delete(int id)
{
return View(AwaitTask(client.GetStoreFromIDAsync(id)));
}
// POST: StoreController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, [Bind("City,Email,Phone,State,StoreName,Street,ZipCode")] Store store)
{
if(id != store.StoreId)
{
return NotFound();
}
try
{
if (ModelState.IsValid)
{
AwaitTask(client.DeleteProductAsync(id));
return RedirectToAction(nameof(Index));
}
}
catch(Exception e)
{
ModelState.AddModelError("", e.Message);
}
return View();
}
}
}
After this you can just right-click on the methods (Index, Details, Delete, Edit, Create) and click AddView
NOTE: in the Index Method you have to change the ActionLinks at the bottom to @azzedinehtmlsql .ActionLink("Edit", "Edit", new { id = item.StoreId}), else it wont work ;)