ارتقاء به ASP.NET Core 1.0 - قسمت 9 - بررسی تغییرات مسیریابی
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۴/۲۳ ۱۲:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
app.UseMvcWithDefaultRoute();
public static IApplicationBuilder UseMvcWithDefaultRoute(this IApplicationBuilder app)
{
if (app == null)
throw new ArgumentNullException("app");
return app.UseMvc((Action<IRouteBuilder>) (routes => routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}")));
} [Route("[controller]/[action]")] namespace Core1RtmEmptyTest.Controllers
{
public class HomeController
{
public string Index()
{
return "Running a POCO controller!";
}
}
} app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
}); using Microsoft.AspNetCore.Mvc;
namespace Core1RtmEmptyTest.Controllers
{
public class AboutController : Controller
{
public ActionResult Hello()
{
return Content("Hello from DNT!");
}
public ActionResult SiteName()
{
return Content("DNT");
}
}
} using Microsoft.AspNetCore.Mvc;
namespace Core1RtmEmptyTest.Controllers
{
[Route("About")]
public class AboutController : Controller
{
[Route("")]
public ActionResult Hello()
{
return Content("Hello from DNT!");
}
[Route("SiteName")]
public ActionResult SiteName()
{
return Content("DNT");
}
}
} AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: Core1RtmEmptyTest.Controllers.AboutController.Hello (Core1RtmEmptyTest) Core1RtmEmptyTest.Controllers.AboutController.SiteName (Core1RtmEmptyTest)
using Microsoft.AspNetCore.Mvc;
namespace Core1RtmEmptyTest.Controllers
{
[Route("[controller]")]
public class AboutController : Controller
{
[Route("")]
public ActionResult Hello()
{
return Content("Hello from DNT!");
}
[Route("[action]")]
public ActionResult SiteName()
{
return Content("DNT");
}
}
} [Route("api/[controller]")] //[Route("/Users/{userid}")]
[Route("Users/{userid}")]
public IActionResult GetUsers(int userId)
{
return Json(new { userId = userId });
} [Route("/Users/{userid:int?}")] [Route("Users/{userid:int}")] [Route("/Users/{userid:int:max(1000):min(10)}")] [Route("/Users/{userid:int}", Name="GetUserById")] string uri = Url.Link("GetUserById", new { userid = 1 }); [Route("/Users/{userid:int}", Name = "GetUserById", Order = 1)] using System;
using System.Globalization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Core1RtmEmptyTest
{
public class CustomRouteConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values,
RouteDirection routeDirection)
{
object value;
if (!values.TryGetValue(routeKey, out value) || value == null)
{
return false;
}
long longValue;
if (value is long)
{
longValue = (long)value;
return longValue != 10;
}
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
if (long.TryParse(valueString, NumberStyles.Integer,
CultureInfo.InvariantCulture, out longValue))
{
return longValue != 10;
}
return false;
}
}
} public class CustomRouteConstraint : IRouteConstraint
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(options =>options.ConstraintMap.Add("Custom", typeof(CustomRouteConstraint))); [Route("/Users/{userid:int:custom}")] app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "spa-fallback",
template: "{*url}",
defaults: new { controller = "Home", action = "Index" });
});
{
"dependencies": {
//same as before
"Microsoft.AspNetCore.SpaServices": "1.0.0-beta-000007"
}, app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
}); // Serve wwwroot as root
app.UseFileServer();
// Serve /node_modules as a separate root (for packages that use other npm modules client side)
app.UseFileServer(new FileServerOptions
{
// Set root of file server
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
// Only react to requests that match this path
RequestPath = "/node_modules",
// Don't expose file system
EnableDirectoryBrowsing = false
}); <Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="1.0.0-beta-000019" />
</ItemGroup>
</Project> <configuration>
<system.webServer>
<handlers>
<add name="SitemapXml" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration> app.UseMvc(routes =>
{
routes.MapRoute(
name: "siteMapRoute",
template: "sitemap.xml",
defaults: new { controller = "Sitemap", action = "index", area = "" }
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
[ActionName("another_name")]
//or
[Route("Home/Contact")] using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.AspNetCore.SpaServices.VueCli;
// ...
namespace Test
{
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
// ...
// In production, the SPA files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseRouting();
app.UseEndpoints(endpoints =>
{
// ...
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
// spa.UseAngularCliServer(npmScript: "start");
// spa.UseVueCliServer(npmScript: "serve");
}
});
}
}
} ASP.NET Core framework APIs that use RegularExpressions pass a timeout.
<aspNetCore requestTimeout="00:00:40"> </aspNetCore>