[英]display url slug instead of id in asp.net core 2.2
實際上,當我刪除某些東西時,我不想看到我的 id。所以我想在這里 slug。但無法理解我如何修改或將 id 轉換為 slug 類型。 這是我的代碼:
啟動.cs
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area=Customer}/{controller=Home}/{action=Index}/{id?}"
);
});
[HttpGet]
public ActionResult Delete(int? id)
{
if (id == null)
{
NotFound();
}
var product = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
if (product == null)
{
return NotFound();
}
return View(product);
}
[HttpPost]
[ActionName("Delete")] //DeleteConfirm nam ke delete namei chinbo
public async Task<IActionResult> DeleteConfirm(int? id)
{
if (id == null)
{
return NotFound();
}
var product = _db.Spray.FirstOrDefault(c => c.Id == id);
if (product == null)
{
return NotFound();
}
_db.Spray.Remove(product);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
}
刪除.cshtml
form asp-action="Delete" method="post" enctype="multipart/form-data">
<div class="p-4 rounded border-row">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div>
<div class="col-8">
<div class="form-group row">
<div class="col-4">
<label asp-for="Name"></label>
</div>
<div class="col-8">
<input type="hidden" asp-for="Id" />
<input asp-for="Name" readonly="readonly" class="form-control" />
</div>
<span asp-validation-for="Name" class="text-danger"></span>
</div>
我不知道如何添加 slug。 我不想看到我的可見身份,因為我試圖制造它的蛞蝓。 但我實際上不明白如何處理。 我是初學者,請幫助任何人。
似乎您想讓 id 在 url 中不可見,簡單的方法是將刪除操作更改為帖子類型。
索引.cshtml:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<form method="post" asp-action="Delete" asp-controller="Sprays">
<input type="hidden" value="@item.Id" name="id" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
}
</tbody>
</table>
Controller:
[HttpPost]
public ActionResult Delete(int? id)
{
//..
}
[HttpPost]
//[ActionName("Delete")]
public async Task<IActionResult> DeleteConfirm(int? id)
{
//...
}
確保您的 Delete.cshtml 應如下所示:
<form asp-action="DeleteConfirm" method="post" enctype="multipart/form-data">
更新:
索引.cshtml:
<form method="post" asp-action="Delete" asp-controller="Sprays" asp-route-slug="my-delete-id">
<input type="hidden" value="@item.Id" name="id" />
<input type="submit" value="Delete" />
</form>
Controller:
[HttpPost]
public async Task<IActionResult> Delete(string slug)
{
var data = HttpContext.Request.Form["id"].First();
var id = int.Parse(data);
//...
}
[HttpPost]
//[ActionName("Delete")]
public async Task<IActionResult> DeleteConfirm(int? id)
{
//...
}
啟動.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{slug?}");
});
整個 Controller:
public class TestsController : Controller
{
private readonly MyDbContext _context;
public TestsController(MyDbContext context)
{
_context = context;
}
// GET: Tests
public async Task<IActionResult> Index()
{
return View(await _context.Test.ToListAsync());
}
// GET: Tests/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var test = await _context.Test
.FirstOrDefaultAsync(m => m.Id == id);
if (test == null)
{
return NotFound();
}
return View(test);
}
// GET: Tests/Create
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name")] Test test)
{
if (ModelState.IsValid)
{
_context.Add(test);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(test);
}
// GET: Tests/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var test = await _context.Test.FindAsync(id);
if (test == null)
{
return NotFound();
}
return View(test);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] Test test)
{
if (id != test.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(test);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TestExists(test.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(test);
}
[HttpPost]
public async Task<IActionResult> Delete(string slug)
{
var data = HttpContext.Request.Form["id"].First();
var id = int.Parse(data);
var test = await _context.Test
.FirstOrDefaultAsync(m => m.Id == id);
if (test == null)
{
return NotFound();
}
return View(test);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var test = await _context.Test.FindAsync(id);
_context.Test.Remove(test);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool TestExists(int id)
{
return _context.Test.Any(e => e.Id == id);
}
}
整個 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{slug?}");
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.